1+ package handlers
2+
3+ import (
4+ "net/http"
5+ "strconv"
6+
7+ "github.com/gin-gonic/gin"
8+ "github.com/go-playground/validator/v10"
9+ "github.com/nevzattalhaozcan/forgotten/internal/models"
10+ "github.com/nevzattalhaozcan/forgotten/internal/services"
11+ )
12+
13+ type BookHandler struct {
14+ bookService * services.BookService
15+ validator * validator.Validate
16+ }
17+
18+ func NewBookHandler (bookService * services.BookService ) * BookHandler {
19+ return & BookHandler {
20+ bookService : bookService ,
21+ validator : validator .New (),
22+ }
23+ }
24+
25+ //TODO: Add swagger comments
26+ func (h * BookHandler ) CreateBook (c * gin.Context ) {
27+ var req models.CreateBookRequest
28+ if err := c .ShouldBindJSON (& req ); err != nil {
29+ c .JSON (http .StatusBadRequest , gin.H {"error" : err .Error ()})
30+ return
31+ }
32+
33+ if err := h .validator .Struct (& req ); err != nil {
34+ c .JSON (http .StatusBadRequest , gin.H {"error" : err .Error ()})
35+ return
36+ }
37+
38+ book , err := h .bookService .CreateBook (& req )
39+ if err != nil {
40+ c .JSON (http .StatusInternalServerError , gin.H {"error" : err .Error ()})
41+ return
42+ }
43+
44+ c .JSON (http .StatusCreated , gin.H {
45+ "message" : "book created successfully" ,
46+ "book" : book ,
47+ })
48+ }
49+
50+ func (h * BookHandler ) GetBookByID (c * gin.Context ) {
51+ idParam := c .Param ("id" )
52+ id , err := strconv .ParseUint (idParam , 10 , 64 )
53+ if err != nil {
54+ c .JSON (http .StatusBadRequest , gin.H {"error" : "invalid book ID" })
55+ return
56+ }
57+
58+ book , err := h .bookService .GetBookByID (uint (id ))
59+ if err != nil {
60+ if err .Error () == "book not found" {
61+ c .JSON (http .StatusNotFound , gin.H {"error" : err .Error ()})
62+ return
63+ }
64+ c .JSON (http .StatusInternalServerError , gin.H {"error" : err .Error ()})
65+ return
66+ }
67+
68+ c .JSON (http .StatusOK , gin.H { "book" : book })
69+ }
70+
71+ func (h * BookHandler ) UpdateBook (c * gin.Context ) {
72+ idParam := c .Param ("id" )
73+ id , err := strconv .ParseUint (idParam , 10 , 64 )
74+ if err != nil {
75+ c .JSON (http .StatusBadRequest , gin.H {"error" : "invalid book ID" })
76+ return
77+ }
78+
79+ var req models.UpdateBookRequest
80+ if err := c .ShouldBindJSON (& req ); err != nil {
81+ c .JSON (http .StatusBadRequest , gin.H {"error" : err .Error ()})
82+ return
83+ }
84+
85+ if err := h .validator .Struct (& req ); err != nil {
86+ c .JSON (http .StatusBadRequest , gin.H {"error" : err .Error ()})
87+ return
88+ }
89+
90+ book , err := h .bookService .UpdateBook (uint (id ), & req )
91+ if err != nil {
92+ if err .Error () == "book not found" {
93+ c .JSON (http .StatusNotFound , gin.H {"error" : err .Error ()})
94+ return
95+ }
96+ c .JSON (http .StatusInternalServerError , gin.H {"error" : err .Error ()})
97+ return
98+ }
99+
100+ c .JSON (http .StatusOK , gin.H {
101+ "message" : "book updated successfully" ,
102+ "book" : book ,
103+ })
104+ }
105+
106+ func (h * BookHandler ) DeleteBook (c * gin.Context ) {
107+ idParam := c .Param ("id" )
108+ id , err := strconv .ParseUint (idParam , 10 , 64 )
109+ if err != nil {
110+ c .JSON (http .StatusBadRequest , gin.H {"error" : "invalid book ID" })
111+ return
112+ }
113+
114+ if err := h .bookService .DeleteBook (uint (id )); err != nil {
115+ if err .Error () == "book not found" {
116+ c .JSON (http .StatusNotFound , gin.H {"error" : err .Error ()})
117+ return
118+ }
119+ c .JSON (http .StatusInternalServerError , gin.H {"error" : err .Error ()})
120+ return
121+ }
122+
123+ c .Status (http .StatusNoContent )
124+ }
125+
126+ func (h * BookHandler ) ListBooks (c * gin.Context ) {
127+ books , err := h .bookService .ListBooks ()
128+ if err != nil {
129+ c .JSON (http .StatusInternalServerError , gin.H {"error" : err .Error ()})
130+ return
131+ }
132+
133+ c .JSON (http .StatusOK , gin.H {"books" : books })
134+ }
0 commit comments