Chore: project init
This commit is contained in:
56
internal/announcement/handler.go
Normal file
56
internal/announcement/handler.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package announcement
|
||||
|
||||
import "github.com/gofiber/fiber/v2"
|
||||
|
||||
type Handler struct {
|
||||
svc *Service
|
||||
}
|
||||
|
||||
func NewHandler(svc *Service) *Handler {
|
||||
return &Handler{svc: svc}
|
||||
}
|
||||
|
||||
func (h *Handler) GetAll(c *fiber.Ctx) error {
|
||||
list, err := h.svc.GetAll()
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "공지사항을 불러오지 못했습니다"})
|
||||
}
|
||||
return c.JSON(list)
|
||||
}
|
||||
|
||||
func (h *Handler) Create(c *fiber.Ctx) error {
|
||||
var body struct {
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
if err := c.BodyParser(&body); err != nil || body.Title == "" || body.Content == "" {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "제목과 내용을 입력해주세요"})
|
||||
}
|
||||
a, err := h.svc.Create(body.Title, body.Content)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "공지사항 생성에 실패했습니다"})
|
||||
}
|
||||
return c.Status(fiber.StatusCreated).JSON(a)
|
||||
}
|
||||
|
||||
func (h *Handler) Update(c *fiber.Ctx) error {
|
||||
var body struct {
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
if err := c.BodyParser(&body); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "잘못된 요청입니다"})
|
||||
}
|
||||
a, err := h.svc.Update(c.Params("id"), body.Title, body.Content)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
return c.JSON(a)
|
||||
}
|
||||
|
||||
func (h *Handler) Delete(c *fiber.Ctx) error {
|
||||
if err := h.svc.Delete(c.Params("id")); err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "삭제에 실패했습니다"})
|
||||
}
|
||||
return c.SendStatus(fiber.StatusNoContent)
|
||||
}
|
||||
Reference in New Issue
Block a user