package service import ( "cc-officialweb/models" "cc-officialweb/utils" ) // GetNews 获取新闻列表,按时间排序获取最近时间的新闻 func GetNews() (new []models.News) { tx := utils.DB.Order("created_at DESC").Find(&new) if tx.Error != nil { return nil } return new } // GetNewsById 根据ID获得新闻 func GetNewsById(id int) (new models.News) { tx := utils.DB.Where("id = ?", id).First(&new) if tx.RowsAffected > 0 { return new } return models.News{} } // GetNewsByTypes 根据类型获取新闻 func GetNewsByTypes(types string) (new models.News) { tx := utils.DB.Where("types = ?", types).First(&new) if tx.RowsAffected > 0 { return new } return models.News{} } // UpdateNews 修改新闻 func UpdateNews(id int, news models.News) bool { tx := utils.DB.Model(&models.News{}).Where("id = ?", id).Updates(news) if tx.RowsAffected > 0 { return true } return false }