86 lines
2.4 KiB
Go
86 lines
2.4 KiB
Go
package controllers
|
|
|
|
import (
|
|
"lost-and-found/internal/models"
|
|
"lost-and-found/internal/services"
|
|
"lost-and-found/internal/utils"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type MatchController struct {
|
|
matchService *services.MatchService
|
|
}
|
|
|
|
func NewMatchController(db *gorm.DB) *MatchController {
|
|
return &MatchController{
|
|
matchService: services.NewMatchService(db),
|
|
}
|
|
}
|
|
|
|
// FindSimilarItems finds similar items for a lost item
|
|
// POST /api/lost-items/:id/find-similar
|
|
func (c *MatchController) FindSimilarItems(ctx *gin.Context) {
|
|
userObj, _ := ctx.Get("user")
|
|
user := userObj.(*models.User)
|
|
|
|
lostItemID, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
|
if err != nil {
|
|
utils.ErrorResponse(ctx, http.StatusBadRequest, "Invalid lost item ID", err.Error())
|
|
return
|
|
}
|
|
|
|
// Only allow managers or the owner to search
|
|
// Add ownership check here if needed
|
|
|
|
results, err := c.matchService.FindSimilarItems(uint(lostItemID))
|
|
if err != nil {
|
|
utils.ErrorResponse(ctx, http.StatusInternalServerError, "Failed to find similar items", err.Error())
|
|
return
|
|
}
|
|
|
|
utils.SuccessResponse(ctx, http.StatusOK, "Similar items found", gin.H{
|
|
"total": len(results),
|
|
"matches": results,
|
|
"user_id": user.ID,
|
|
})
|
|
}
|
|
|
|
// GetMatchesForLostItem gets all matches for a lost item
|
|
// GET /api/lost-items/:id/matches
|
|
func (c *MatchController) GetMatchesForLostItem(ctx *gin.Context) {
|
|
lostItemID, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
|
if err != nil {
|
|
utils.ErrorResponse(ctx, http.StatusBadRequest, "Invalid lost item ID", err.Error())
|
|
return
|
|
}
|
|
|
|
matches, err := c.matchService.GetMatchesForLostItem(uint(lostItemID))
|
|
if err != nil {
|
|
utils.ErrorResponse(ctx, http.StatusInternalServerError, "Failed to get matches", err.Error())
|
|
return
|
|
}
|
|
|
|
utils.SuccessResponse(ctx, http.StatusOK, "Matches retrieved", matches)
|
|
}
|
|
|
|
// GetMatchesForItem gets all matches for an item
|
|
// GET /api/items/:id/matches
|
|
func (c *MatchController) GetMatchesForItem(ctx *gin.Context) {
|
|
itemID, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
|
if err != nil {
|
|
utils.ErrorResponse(ctx, http.StatusBadRequest, "Invalid item ID", err.Error())
|
|
return
|
|
}
|
|
|
|
matches, err := c.matchService.GetMatchesForItem(uint(itemID))
|
|
if err != nil {
|
|
utils.ErrorResponse(ctx, http.StatusInternalServerError, "Failed to get matches", err.Error())
|
|
return
|
|
}
|
|
|
|
utils.SuccessResponse(ctx, http.StatusOK, "Matches retrieved", matches)
|
|
} |