Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:新增失物招领接口 #22

Merged
merged 32 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
b808f2f
chore(model):修改失物招领数据库模板
Tim3088 Nov 21, 2024
fe3748b
feat:新增发布失物招领接口
Tim3088 Nov 21, 2024
4819afe
feat:新增撤回失物招领接口
Tim3088 Nov 21, 2024
9cd24bc
feat:新增审核失物招领接口
Tim3088 Nov 21, 2024
c7054b3
feat:新增修改失物招领接口
Tim3088 Nov 21, 2024
8baea7b
feat:新增获取失物招领列表接口
Tim3088 Nov 21, 2024
ccfda81
feat:新增获取联系方式接口
Tim3088 Nov 21, 2024
1fe6952
feat:新增获取最新的失物招领接口
Tim3088 Nov 21, 2024
b54d5ab
feat:新增查看发布失物招领信息后的审核状态接口
Tim3088 Nov 21, 2024
492fb59
Merge branch 'zjutjh:dev' into dev
Tim3088 Nov 21, 2024
191b182
chore:修改审核失物招领数据库操作
Tim3088 Nov 21, 2024
0d05628
chore(model):修改失物招领记录结构体
Tim3088 Nov 21, 2024
274c039
fix:修改获取失物招领列表判断逻辑
Tim3088 Nov 21, 2024
c25e5c6
fix:修改获取最新失物招领判断逻辑
Tim3088 Nov 21, 2024
4f727b2
fix:修改查看失物招领信息的状态接口
Tim3088 Nov 21, 2024
8b5ee4a
fix:新增用户设置失物招领为已完成接口
Tim3088 Nov 22, 2024
ce312f7
Merge remote-tracking branch 'origin/dev' into dev
Tim3088 Nov 26, 2024
2edcc8c
chore:修复代码格式问题
Tim3088 Nov 26, 2024
6183953
fix(controllers): 审核失物招领增加else
Tim3088 Nov 29, 2024
d59794b
refactor(router): 重构初始化代码并调整路由组
Tim3088 Nov 29, 2024
04a0342
Merge branch 'dev' into Tim3088/dev
Tim3088 Nov 29, 2024
2cc3fa9
Merge remote-tracking branch 'origin/dev' into Tim3088/dev
Tim3088 Dec 3, 2024
b660e5c
feat(utils):新增字符串和字符串数组转换器
Tim3088 Dec 3, 2024
4529d31
Merge remote-tracking branch 'origin/dev' into Tim3088/dev
Tim3088 Dec 3, 2024
963203c
refactor: 优化创建失物招领接口的入参和图片url存储方式
Tim3088 Dec 3, 2024
ca606db
refactor: 优化获取失物招领接口的出入参和图片url存储方式
Tim3088 Dec 3, 2024
f2f455f
refactor(models): 优化失物招领记录的结构体
Tim3088 Dec 3, 2024
d789166
fix(router): 优化代码格式问题
Tim3088 Dec 3, 2024
b479440
Merge branch 'dev' into Tim3088/dev
Penryn Dec 10, 2024
3058fab
refactor(app/utils): 优化 StringsToString 函数
Tim3088 Dec 11, 2024
008bb79
style(config): 调整路由定义格式
Tim3088 Dec 11, 2024
2102f9a
refactor(lostAndFound):调整失物招领创建和更新接口参数
Tim3088 Dec 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions app/controllers/lostAndFoundController/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package lostAndFoundController

import (
"4u-go/app/apiException"
"4u-go/app/models"
"4u-go/app/services/lostAndFoundService"
"4u-go/app/utils"
"github.com/gin-gonic/gin"
)

type createLostAndFoundData struct {
Type bool `json:"type"` // 1-失物 0-寻物
Name string `json:"name" binding:"required"` // 物品名称
Introduction string `json:"introduction" binding:"required"` // 物品介绍
Campus uint8 `json:"campus"` // 校区 0-其他 1-朝晖 2-屏峰 3-莫干山
Kind uint8 `json:"kind"` // 物品种类 1其他2证件3箱包4首饰5现金6电子产品7钥匙
Place string `json:"place" binding:"required"` // 丢失或拾得地点
Time string `json:"time" binding:"required"` // 丢失或拾得时间
Imgs []string `json:"imgs" binding:"required"` // 物品图片,多个图片以逗号分隔
Tim3088 marked this conversation as resolved.
Show resolved Hide resolved
Contact string `json:"contact" binding:"required"` // 联系方式
ContactWay uint8 `json:"contact_way" binding:"required"` // 联系方式选项 1-手机号 2-qq 3-微信 4-邮箱
}

// CreateLostAndFound 创建一条失物招领
func CreateLostAndFound(c *gin.Context) {
var data createLostAndFoundData
err := c.ShouldBindJSON(&data)
if err != nil {
apiException.AbortWithException(c, apiException.ParamError, err)
return
}

// 判断imgs是否大于9
if len(data.Imgs) > 9 {
apiException.AbortWithException(c, apiException.ParamError, nil)
return
}

// 将[]string转为string
imgs := utils.StringsToString(data.Imgs)

err = lostAndFoundService.SaveLostAndFound(models.LostAndFoundRecord{
Type: data.Type,
Name: data.Name,
Introduction: data.Introduction,
Campus: data.Campus,
Kind: data.Kind,
Place: data.Place,
Time: data.Time,
Imgs: imgs,
Publisher: utils.GetUser(c).StudentID,
Contact: data.Contact,
ContactWay: data.ContactWay,
IsProcessed: 2,
IsApproved: 2,
})
if err != nil {
apiException.AbortWithException(c, apiException.ServerError, err)
return
}

utils.JsonSuccessResponse(c, nil)
}
51 changes: 51 additions & 0 deletions app/controllers/lostAndFoundController/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package lostAndFoundController

import (
"errors"

"4u-go/app/apiException"
"4u-go/app/models"
"4u-go/app/services/lostAndFoundService"
"4u-go/app/utils"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)

type deleteLostAndFoundData struct {
ID uint `json:"id" binding:"required"`
}

// DeleteLostAndFound 撤回一条失物招领
func DeleteLostAndFound(c *gin.Context) {
var data deleteLostAndFoundData
err := c.ShouldBindJSON(&data)
if err != nil {
apiException.AbortWithException(c, apiException.ParamError, err)
return
}

// 判断失物招领是否存在
record, err := lostAndFoundService.GetLostAndFoundById(data.ID)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
apiException.AbortWithException(c, apiException.ResourceNotFound, err)
} else {
apiException.AbortWithException(c, apiException.ServerError, err)
}
return
}

user := utils.GetUser(c)
if user.Type != models.SuperAdmin && user.Type != models.ForU && user.StudentID != record.Publisher {
apiException.AbortWithException(c, apiException.NotPermission, nil)
return
}

err = lostAndFoundService.DeleteLostAndFoundById(data.ID)
if err != nil {
apiException.AbortWithException(c, apiException.ServerError, err)
return
}

utils.JsonSuccessResponse(c, nil)
}
168 changes: 168 additions & 0 deletions app/controllers/lostAndFoundController/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package lostAndFoundController

import (
"errors"

"4u-go/app/apiException"
"4u-go/app/services/lostAndFoundService"
"4u-go/app/utils"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)

type getLostAndFoundListData struct {
Type bool `json:"type"` // 1-失物 0-寻物
Campus uint8 `json:"campus"` // 校区 0-其他 1-朝晖 2-屏峰 3-莫干山
Kind uint8 `json:"kind"` // 物品种类 0-全部 1-其他 2-饭卡 3-电子 4-文体 5-衣包 6-证件
}
type getLostAndFoundListResponse struct {
LostAndFoundList []lostAndFoundElement `json:"list"`
}
type lostAndFoundElement struct {
ID uint `json:"id"`
Imgs []string `json:"imgs"`
Name string `json:"name"`
Place string `json:"place"`
Time string `json:"time"`
Introduction string `json:"introduction"`
Kind uint8 `json:"kind"`
}

// GetLostAndFoundList 获取失物招领列表
func GetLostAndFoundList(c *gin.Context) {
var data getLostAndFoundListData
err := c.ShouldBindJSON(&data)
if err != nil {
apiException.AbortWithException(c, apiException.ParamError, err)
return
}

list, err := lostAndFoundService.GetLostAndFoundList(data.Type, data.Campus, data.Kind)
if err != nil {
apiException.AbortWithException(c, apiException.ServerError, err)
return
}

lostAndFoundList := make([]lostAndFoundElement, 0)
for _, record := range list {
// 将string转为[]string
imgs := utils.StringToStrings(record.Imgs)
lostAndFoundList = append(lostAndFoundList, lostAndFoundElement{
ID: record.ID,
Imgs: imgs,
Name: record.Name,
Place: record.Place,
Time: record.Time,
Introduction: record.Introduction,
})
}

utils.JsonSuccessResponse(c, getLostAndFoundListResponse{
LostAndFoundList: lostAndFoundList,
})
}

type getLostAndFoundContentData struct {
ID uint `json:"id" binding:"required"`
}

// GetLostAndFoundContact 获取失物招领联系方式
func GetLostAndFoundContact(c *gin.Context) {
var data getLostAndFoundContentData
err := c.ShouldBindJSON(&data)
if err != nil {
apiException.AbortWithException(c, apiException.ParamError, err)
return
}

contact, err := lostAndFoundService.GetLostAndFoundContact(data.ID, utils.GetUser(c).StudentID)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
apiException.AbortWithException(c, apiException.ResourceNotFound, err)
} else {
apiException.AbortWithException(c, apiException.ServerError, err)
}
return
}

utils.JsonSuccessResponse(c, contact)
}

type latestLostAndFoundResponse struct {
Type bool `json:"type"`
Imgs string `json:"imgs"`
Name string `json:"name"`
Place string `json:"place"`
Introduction string `json:"introduction"`
}

// GetLatestLostAndFound 获取最新失物招领
func GetLatestLostAndFound(c *gin.Context) {
record, err := lostAndFoundService.GetLatestLostAndFound()
if err != nil {
apiException.AbortWithException(c, apiException.ServerError, err)
return
}

utils.JsonSuccessResponse(c, latestLostAndFoundResponse{
Type: record.Type,
Imgs: record.Imgs,
Name: record.Name,
Place: record.Place,
Introduction: record.Introduction,
})
}

type getLostAndFoundStatusData struct {
Status uint8 `json:"status"` // 状态 0-已撤回 1-已审核 2-审核中
}
type getLostAndFoundStatusResponse struct {
List []lostAndFoundStatusElement `json:"list"`
}
type lostAndFoundStatusElement struct {
ID uint `json:"id"`
Type bool `json:"type"`
Imgs []string `json:"imgs"`
Name string `json:"name"`
Kind uint8 `json:"kind"`
Place string `json:"place"`
Time string `json:"time"`
Introduction string `json:"introduction"`
IsApproved uint8 `json:"is_approved"`
}

// GetUserLostAndFoundStatus 查看失物招领信息的状态
func GetUserLostAndFoundStatus(c *gin.Context) {
var data getLostAndFoundStatusData
err := c.ShouldBindJSON(&data)
if err != nil {
apiException.AbortWithException(c, apiException.ParamError, err)
return
}

list, err := lostAndFoundService.GetUserLostAndFoundStatus(utils.GetUser(c).StudentID, data.Status)
if err != nil {
apiException.AbortWithException(c, apiException.ServerError, err)
return
}

lostAndFoundList := make([]lostAndFoundStatusElement, 0)
for _, record := range list {
// 将string转为[]string
imgs := utils.StringToStrings(record.Imgs)
lostAndFoundList = append(lostAndFoundList, lostAndFoundStatusElement{
ID: record.ID,
Type: record.Type,
Imgs: imgs,
Name: record.Name,
Kind: record.Kind,
Place: record.Place,
Time: record.Time,
Introduction: record.Introduction,
IsApproved: record.IsApproved,
})
}
utils.JsonSuccessResponse(c, getLostAndFoundStatusResponse{
List: lostAndFoundList,
})
}
Loading
Loading