当前位置: 首页 > news >正文

Go-web开发之社区功能

社区功能

查询社区列表

community.go

func (Community) TableName() string 用于指定数据库表名

type Community struct {ID            int64     `json:"id" gorm:"primary_key;auto_increment"`CommunityID   string    `json:"community_id" gorm:"unique;not null"` // 社区IDCommunityName string    `json:"community_name" gorm:"not null"`      // 社区名称Introduction  string    `json:"introduction"`                        // 社区简介CreateTime    time.Time `json:"create_time"`                         // 创建时间UpdateTime    time.Time `json:"update_time"`                         // 更新时间
}func (Community) TableName() string {return "community"
}

route.go

	r.Use(middleware.JWTAuthMiddleware()){r.GET("/community", controller.CommunityHandler)r.GET("/community/:id", controller.CommunityDetailHandler)}

comunityController.go

func CommunityHandler(c *gin.Context) {communityList, err := logic.GetCommunityList()if err != nil {c.JSON(http.StatusOK, gin.H{"msg": "获取社区列表失败" + err.Error(),})return}c.JSON(http.StatusOK, gin.H{"msg":  "获取社区列表成功","data": communityList,})}
func CommunityDetailHandler(c *gin.Context) {// 1. 获取参数idStr := c.Param("id")id, err2 := strconv.ParseInt(idStr, 10, 64)if err2 != nil {return}// 2. 业务逻辑community, err := logic.GetCommunityDetail(id)if err != nil {c.JSON(http.StatusOK, gin.H{"msg": "获取社区详情失败" + err.Error(),})return}// 3. 返回数据c.JSON(http.StatusOK, gin.H{"msg":  "获取社区详情成功","data": community,})
}

communityLogic.go

func GetCommunityList() ([]models.Community, error) {// 1. 查询数据库	2. 返回数据return mysql.QueryCommunity()
}func GetCommunityDetail(id int64) (*models.Community, error) {// 1. 查询数据库return mysql.QueryCommunityById(id)
}

communityDao.go

func QueryCommunity() ([]models.Community, error) {// 1. 查询数据库var communityList []models.Communityerr := db.Select("community_id, community_name").Order("community_id desc").Find(&communityList).Errorreturn communityList, err
}func QueryCommunityById(id int64) (*models.Community, error) {var community models.Communityerr := db.Select("community_id, community_name, introduction").Where("community_id = ?", id).First(&community).Errorif err != nil {return nil, err}return &community, nil
}

相关文章:

  • B站Michale_ee——ESP32_IDF SDK——FreeRTOS_2 队列
  • 2025大模型微调视频课程全套(附下载)
  • 2025年渗透测试面试题总结-拷打题库30(题目+回答)
  • Curl 全面使用指南
  • node.js模块化步骤(各标准区别)CommonJS规范、AMD规范、UMD规范、ES Modules (ESM)
  • 小刚说C语言刷题—1602总分和平均分
  • 基于若依RuoYi-Vue3-FastAPI 的 Docker 部署记录
  • 驱动开发系列55 - Linux Graphics QXL显卡驱动代码分析(二)显存管理
  • 《Android 应用开发基础教程》——第十章:使用 Gson 实现网络 JSON 数据解析与对象映射
  • RAGFlow报错:ESConnection.sql got exception
  • 纯html实现的json数据转csv文件
  • 题解:洛谷 CF2091E Interesting Ratio
  • 猫,为什么是猫?
  • Y1代码AC集
  • Python协程入门指北
  • 数据结构与算法学习笔记(Acwing提高课)----动态规划·背包模型(一)
  • 奥地利学派方法论的三个基础
  • Python TensorFlow库【深度学习框架】全面讲解与案例
  • 从MCP基础到FastMCP实战应用
  • 安全学习基础入门5集
  • 科普|“小石头,大麻烦”,出现输尿管结石如何应对?
  • 习近平主持召开部分省区市“十五五”时期经济社会发展座谈会
  • 招商蛇口:一季度营收约204亿元,净利润约4.45亿元
  • 笔墨如何“构城”?上海美院城市山水晋京展出
  • 被算法重塑的世界,人与技术如何和谐共处
  • 赛力斯拟赴港上市:去年扭亏为盈净利59亿元,三年内实现百万销量目标