diff --git a/backend/api/blogs.go b/backend/api/blogs.go index 3bcffbb..2b1ec62 100644 --- a/backend/api/blogs.go +++ b/backend/api/blogs.go @@ -2,57 +2,43 @@ package api import ( "context" - "encoding/json" "fmt" - "log" "net/http" - "time" core "fr.latosa-escrima/api/core" - "github.com/uptrace/bun" ) func HandleGetBlog(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - fmt.Println("salut") - emptyObject := make(map[string]interface{}) - - emptyJSON, json_err := json.Marshal(emptyObject) - if json_err != nil { - fmt.Println("Couldn't create the json object") - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(emptyJSON)) - return - } + w.Header().Set("Content-Type", "application/json") if r.Method != http.MethodGet { - http.Error(w, "Wrong method", http.StatusMethodNotAllowed) + core.JSONError{ + Status: core.Error, + Message: "Method not Allowed", + }.Respond(w, http.StatusMethodNotAllowed) return } - blog := &core.Blog{ - BaseModel: bun.BaseModel{}, - BlogID: [16]byte{}, - Slug: "", - Content: "", - Label: "", - AuthorID: [16]byte{}, - Published: time.Time{}, - Summary: "", - Image: "", - Href: "", - Author: &core.User{}, - } - blog_uuid := r.PathValue("uuid") - fmt.Println(blog_uuid) - ctx := context.Background() - err_db := core.DB.NewSelect().Model(blog).Where("uuid = ?", blog_uuid).Scan(ctx) - if err_db != nil { - log.Fatal(err_db) - http.Error(w, "Can't use select", http.StatusNotFound) - return + + var blog core.Blog + _, err := core.DB.NewSelect(). + Model(&blog). + Where("blog_id = ?", blog_uuid). + Relation("Author"). + ScanAndCount(context.Background()) + if err != nil { + core.JSONError{ + Status: core.Error, + Message: err.Error(), + }.Respond(w, http.StatusNotAcceptable) + return } - - w.Write([]byte(`{message: "Successfuly responded to request}`)) + + core.JSONSuccess{ + Status: core.Success, + Message: "Status OK", + Data: blog, + }.Respond(w, http.StatusOK) + return } diff --git a/backend/api/core/schemas.go b/backend/api/core/schemas.go index 92062c7..f983341 100644 --- a/backend/api/core/schemas.go +++ b/backend/api/core/schemas.go @@ -33,6 +33,13 @@ const ( UserRole Role = "user" ) +type Status string + +const ( + Active Status = "Active" + Inactive Status = "Inactive" +) + type User struct { bun.BaseModel `bun:"table:users"` @@ -52,17 +59,17 @@ type User struct { type Event struct { bun.BaseModel `bun:"table:events"` - EventID uuid.UUID `bun:"type:uuid,pk"` - CreationDate time.Time `bun:"creation_date,notnull,default:current_timestamp"` - ScheduleStart time.Time `bun:"schedule_start,notnull"` - ScheduleEnd time.Time `bun:"schedule_end,notnull"` - Status string `bun:"status,notnull"` + EventID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()" json:"eventID"` + CreationDate time.Time `bun:"creation_date,notnull,default:current_timestamp" json:"creationDate"` + ScheduleStart time.Time `bun:"schedule_start,notnull" json:"scheduleStart"` + ScheduleEnd time.Time `bun:"schedule_end,notnull" json:"scheduleEnd"` + Status Status `bun:"status,notnull,default:Inactive" json:"status"` } type EventToUser struct { bun.BaseModel `bun:"table:events_to_users"` - EventID uuid.UUID `bun:"type:uuid,pk"` + EventID uuid.UUID `bun:"type:uuid,pk"` UserID uuid.UUID `bun:"type:uuid,pk"` Event *Event `bun:"rel:belongs-to,join:event_id=event_id"` @@ -72,24 +79,24 @@ type EventToUser struct { type Blog struct { bun.BaseModel `bun:"table:blogs"` - BlogID uuid.UUID `bun:"type:uuid,pk"` - Slug string `bun:"slug,unique,notnull"` - Content string `bun:"content,notnull"` - Label string `bun:"label"` - AuthorID uuid.UUID `bun:"author_id,notnull"` - Published time.Time `bun:"published,default:current_timestamp"` - Summary string `bun:"summary"` - Image string `bun:"image"` - Href string `bun:"href"` + BlogID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()" json:"blogID"` + Slug string `bun:"slug,unique,notnull" json:"slug"` + Content string `bun:"content,notnull" json:"content"` + Label string `bun:"label" json:"label"` + AuthorID uuid.UUID `bun:"author_id,type:uuid,notnull" json:"authorID"` + Published time.Time `bun:"published,default:current_timestamp" json:"published"` + Summary string `bun:"summary" json:"summary"` + Image string `bun:"image" json:"image"` + Href string `bun:"href" json:"href"` - Author *User `bun:"rel:belongs-to,join:author_id=user_id"` + Author User `bun:"rel:belongs-to,join:author_id=user_id" json:"author"` } type WebsiteSettings struct { bun.BaseModel `bun:"table:website_settings"` - ID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()"` - AutoAcceptDemand bool `bun:"auto_accept_demand,default:false"` + ID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()" json:"id"` + AutoAcceptDemand bool `bun:"auto_accept_demand,default:false" json:"autoAcceptDemand"` } func InitDatabase(dsn DSN) (*bun.DB, error) { diff --git a/backend/api/event.go b/backend/api/event.go new file mode 100644 index 0000000..b2a1336 --- /dev/null +++ b/backend/api/event.go @@ -0,0 +1,40 @@ +package api + +import ( + "context" + "encoding/json" + "net/http" + + core "fr.latosa-escrima/api/core" +) + +func HandleGetEvent(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + + if r.Method != http.MethodGet { + core.JSONError{ + Status: core.Error, + Message: "Method not Allowed", + }.Respond(w, http.StatusMethodNotAllowed) + return + } + + event_uuid := r.PathValue("uuid") + + var event core.Event + _, err := core.DB.NewSelect().Model(&event).Where("uuid = ?", event_uuid).ScanAndCount(context.Background()) + if err != nil { + core.JSONError{ + Status: core.Error, + Message: err.Error(), + }.Respond(w, http.StatusNotAcceptable) + return + } + + core.JSONSuccess{ + Status: core.Success, + Message: "Event Returned", + Data: event, + }.Respond(w, http.StatusOK) + return +} diff --git a/backend/api/new_blog.go b/backend/api/new_blog.go index 3151747..9b1150f 100644 --- a/backend/api/new_blog.go +++ b/backend/api/new_blog.go @@ -3,50 +3,49 @@ package api import ( "context" "encoding/json" - "fmt" - "log" + "io" "net/http" - "time" core "fr.latosa-escrima/api/core" - "github.com/uptrace/bun" ) func HandleCreateBlog(w http.ResponseWriter, r *http.Request) { - - emptyObject := make(map[string]interface{}) - - emptyJSON, json_err := json.Marshal(emptyObject) - if json_err != nil { - fmt.Println("Couldn't create the json object") - w.Write(emptyJSON) - } - if r.Method != http.MethodPost { + core.JSONError{ + Status: core.Error, + Message: "Method is not allowed", + }.Respond(w, http.StatusMethodNotAllowed) + return } - err := r.ParseForm() + body, err := io.ReadAll(r.Body) if err != nil { + core.JSONError{ + Status: core.Error, + Message: err.Error(), + }.Respond(w, http.StatusNoContent) + return + } + var blog core.Blog + if err = json.Unmarshal(body, &blog); err != nil { + core.JSONError{ + Status: core.Error, + Message: err.Error(), + }.Respond(w, http.StatusNoContent) + return + } + + _, err = core.DB.NewInsert().Model(blog).Exec(context.Background()) + if err != nil { + core.JSONError{ + Status: core.Error, + Message: err.Error(), + }.Respond(w, http.StatusNotAcceptable) } - user := &core.Blog{ - BaseModel: bun.BaseModel{}, - BlogID: [16]byte{}, - Slug: "", - Content: "", - Label: "", - AuthorID: [16]byte{}, - Published: time.Time{}, - Summary: "", - Image: "", - Href: "", - Author: &core.User{}, - } - - _, err_db := core.DB.NewInsert().Model(user).Exec(context.Background()) - if err_db != nil { - log.Fatal(err) - } - - fmt.Println("User inserted successfully") + core.JSONSuccess{ + Status: core.Success, + Message: "Blog inserted", + Data: blog, + }.Respond(w, http.StatusCreated) } diff --git a/backend/api/new_event.go b/backend/api/new_event.go new file mode 100644 index 0000000..cefd342 --- /dev/null +++ b/backend/api/new_event.go @@ -0,0 +1,51 @@ +package api + +import ( + "context" + "net/http" + "io" + "encoding/json" + + core "fr.latosa-escrima/api/core" +) + +func HandleCreateEvent(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + core.JSONError{ + Status: core.Error, + Message: "Method is not allowed", + }.Respond(w, http.StatusMethodNotAllowed) + return + } + + body, err := io.ReadAll(r.Body) + if err != nil { + core.JSONError{ + Status: core.Error, + Message: err.Error(), + }.Respond(w, http.StatusNoContent) + return + } + var event core.Event + if err = json.Unmarshal(body, &event); err != nil { + core.JSONError{ + Status: core.Error, + Message: err.Error(), + }.Respond(w, http.StatusNoContent) + return + } + + _, err = core.DB.NewInsert().Model(&event).Exec(context.Background()) + if err != nil { + core.JSONError{ + Status: core.Error, + Message: err.Error(), + }.Respond(w, http.StatusNotAcceptable) + } + + core.JSONSuccess{ + Status: core.Success, + Message: "Event inserted", + Data: event, + }.Respond(w, http.StatusCreated) +}