Skip to content

Commit

Permalink
Merge pull request #22 from zjutjh/Tim3088/dev
Browse files Browse the repository at this point in the history
feat:新增失物招领接口
  • Loading branch information
cbluebird authored Dec 16, 2024
2 parents 6976894 + 2102f9a commit bb86b60
Show file tree
Hide file tree
Showing 13 changed files with 617 additions and 14 deletions.
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"` // 物品图片,多个图片以逗号分隔
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

0 comments on commit bb86b60

Please sign in to comment.