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

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
}
http://www.xdnf.cn/news/3404.html

相关文章:

  • Java 中那些奇怪的空指针报错场景及解决方案NullPointerException
  • 【计算机视觉】语义分割:MMSegmentation:OpenMMLab开源语义分割框架实战指南
  • MySQL数据同步之Canal讲解
  • 2025年- H16-Lc124-169.多数元素(技巧)---java版
  • 7.0/Q1,GBD数据库最新文章解读
  • ClackyAI:下一代智能云开发环境的技术革新与实践价值
  • WPF使用依赖注入框架AutoMapper
  • 仿腾讯会议——服务器结构讲解
  • Matlab/Simulink - BLDC直流无刷电机仿真基础教程(四) - PWM调制模拟
  • 后端工程师需要掌握哪些基础技能
  • 机器人--底盘
  • 人才答辩ppt优化技巧_杰青_优青_万人计划青年拔尖人才_青年长江学者ppt制作案例
  • 2025五一杯A题五一杯数学建模思路代码文章教学:支路车流量推测问题
  • 部署.NET6.0 Web API项目到Docker
  • 实现了一个基于寄存器操作STM32F103C8t6的工程, 并实现对PA1,PA2接LED正极的点灯操作
  • npm宿主依赖、宿主环境依赖(peerDependencies)(指由宿主环境提供的依赖)
  • 网络安全防火墙技术有哪些?网络防火墙的主要作用
  • 在ASP.NET MVC中使用Repeater指南
  • 【浅尝Java】Java简介第一个Java程序(含JDK、JRE与JVM关系、javcdoc的使用)
  • Seata服务端回滚事务核心源码解析
  • springboot中异步接口实现所有方式_20250501
  • 内存 “舞台” 上,进程如何 “翩翩起舞”?(转)
  • idea安装
  • 【Unity】 组件库分类详解
  • RAGFlow报错:ESConnection.sql got exception
  • 【基础算法】插值查找算法 - JAVA
  • (即插即用模块-Attention部分) 六十一、(2024 ACCV) LIA 基于局部重要性的注意力
  • 【数据分享】2020年中国高精度森林覆盖数据集(免费获取)
  • VBA数据库解决方案第二十讲:Select From Where条件表达式
  • 「面白い」日本 课文详解