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

Tim3088/dev #21

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
52 changes: 52 additions & 0 deletions app/controllers/lostAndFoundController/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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" binding:"required"` // 校区 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"` // 物品图片,多个图片以逗号分隔
Contact string `json:"contact" binding:"required"` // 联系方式
}

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

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: data.Imgs,
Publisher: utils.GetUser(c).StudentID,
Contact: data.Contact,
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)
}
164 changes: 164 additions & 0 deletions app/controllers/lostAndFoundController/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
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" binding:"required"` // 校区 1-朝晖 2-屏峰 3-莫干山
Kind uint8 `json:"kind"` // 物品种类 0全部1其他2证件3箱包4首饰5现金6电子产品7钥匙
}
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"`
}

// 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 {
lostAndFoundList = append(lostAndFoundList, lostAndFoundElement{
ID: record.ID,
Imgs: record.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 {
lostAndFoundList = append(lostAndFoundList, lostAndFoundStatusElement{
ID: record.ID,
Type: record.Type,
Imgs: record.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