12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package service
- import (
- "cc-officialweb/models"
- "cc-officialweb/utils"
- )
- // GetNews 获取新闻列表,按时间排序获取最近时间的新闻
- func GetNews(types string) (new []models.News) {
- tx := utils.DB.Where("types=?", types).Order("created_at DESC").Find(&new)
- if tx.RowsAffected > 0 {
- return new
- }
- return
- }
- // 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{}
- }
- func AddNews(newsdto models.NewsDto) bool {
- news := models.News{
- Title: newsdto.Title,
- Synopsis: newsdto.Synopsis,
- Detail: newsdto.Detail,
- Types: newsdto.Types,
- IsActive: newsdto.IsActive,
- Image: newsdto.Image,
- }
- tx := utils.DB.Create(&news)
- if tx.RowsAffected > 0 {
- return true
- }
- return false
- }
- // 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
- }
|