68 lines
1.9 KiB
Go
68 lines
1.9 KiB
Go
package controllers
|
|
|
|
import (
|
|
"lost-and-found/internal/services"
|
|
"lost-and-found/internal/utils"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type ArchiveController struct {
|
|
archiveService *services.ArchiveService
|
|
}
|
|
|
|
func NewArchiveController(db *gorm.DB) *ArchiveController {
|
|
return &ArchiveController{
|
|
archiveService: services.NewArchiveService(db),
|
|
}
|
|
}
|
|
|
|
// GetAllArchives gets all archived items
|
|
// GET /api/archives
|
|
func (c *ArchiveController) GetAllArchives(ctx *gin.Context) {
|
|
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
|
|
limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
|
|
reason := ctx.Query("reason")
|
|
search := ctx.Query("search")
|
|
|
|
archives, total, err := c.archiveService.GetAllArchives(page, limit, reason, search)
|
|
if err != nil {
|
|
utils.ErrorResponse(ctx, http.StatusInternalServerError, "Failed to get archives", err.Error())
|
|
return
|
|
}
|
|
|
|
utils.SendPaginatedResponse(ctx, http.StatusOK, "Archives retrieved", archives, total, page, limit)
|
|
}
|
|
|
|
// GetArchiveByID gets archive by ID
|
|
// GET /api/archives/:id
|
|
func (c *ArchiveController) GetArchiveByID(ctx *gin.Context) {
|
|
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
|
if err != nil {
|
|
utils.ErrorResponse(ctx, http.StatusBadRequest, "Invalid archive ID", err.Error())
|
|
return
|
|
}
|
|
|
|
archive, err := c.archiveService.GetArchiveByID(uint(id))
|
|
if err != nil {
|
|
utils.ErrorResponse(ctx, http.StatusNotFound, "Archive not found", err.Error())
|
|
return
|
|
}
|
|
|
|
utils.SuccessResponse(ctx, http.StatusOK, "Archive retrieved", archive.ToResponse())
|
|
}
|
|
|
|
// GetArchiveStats gets archive statistics
|
|
// GET /api/archives/stats
|
|
func (c *ArchiveController) GetArchiveStats(ctx *gin.Context) {
|
|
stats, err := c.archiveService.GetArchiveStats()
|
|
if err != nil {
|
|
utils.ErrorResponse(ctx, http.StatusInternalServerError, "Failed to get stats", err.Error())
|
|
return
|
|
}
|
|
|
|
utils.SuccessResponse(ctx, http.StatusOK, "Archive stats retrieved", stats)
|
|
} |