blogs and event api : Get and Post methods
This commit is contained in:
@@ -2,57 +2,43 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
core "fr.latosa-escrima/api/core"
|
core "fr.latosa-escrima/api/core"
|
||||||
"github.com/uptrace/bun"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleGetBlog(w http.ResponseWriter, r *http.Request) {
|
func HandleGetBlog(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
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
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.Method != http.MethodGet {
|
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
|
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")
|
blog_uuid := r.PathValue("uuid")
|
||||||
fmt.Println(blog_uuid)
|
|
||||||
ctx := context.Background()
|
var blog core.Blog
|
||||||
err_db := core.DB.NewSelect().Model(blog).Where("uuid = ?", blog_uuid).Scan(ctx)
|
_, err := core.DB.NewSelect().
|
||||||
if err_db != nil {
|
Model(&blog).
|
||||||
log.Fatal(err_db)
|
Where("blog_id = ?", blog_uuid).
|
||||||
http.Error(w, "Can't use select", http.StatusNotFound)
|
Relation("Author").
|
||||||
return
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,13 @@ const (
|
|||||||
UserRole Role = "user"
|
UserRole Role = "user"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Status string
|
||||||
|
|
||||||
|
const (
|
||||||
|
Active Status = "Active"
|
||||||
|
Inactive Status = "Inactive"
|
||||||
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
bun.BaseModel `bun:"table:users"`
|
bun.BaseModel `bun:"table:users"`
|
||||||
|
|
||||||
@@ -52,17 +59,17 @@ type User struct {
|
|||||||
type Event struct {
|
type Event struct {
|
||||||
bun.BaseModel `bun:"table:events"`
|
bun.BaseModel `bun:"table:events"`
|
||||||
|
|
||||||
EventID uuid.UUID `bun:"type:uuid,pk"`
|
EventID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()" json:"eventID"`
|
||||||
CreationDate time.Time `bun:"creation_date,notnull,default:current_timestamp"`
|
CreationDate time.Time `bun:"creation_date,notnull,default:current_timestamp" json:"creationDate"`
|
||||||
ScheduleStart time.Time `bun:"schedule_start,notnull"`
|
ScheduleStart time.Time `bun:"schedule_start,notnull" json:"scheduleStart"`
|
||||||
ScheduleEnd time.Time `bun:"schedule_end,notnull"`
|
ScheduleEnd time.Time `bun:"schedule_end,notnull" json:"scheduleEnd"`
|
||||||
Status string `bun:"status,notnull"`
|
Status Status `bun:"status,notnull,default:Inactive" json:"status"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventToUser struct {
|
type EventToUser struct {
|
||||||
bun.BaseModel `bun:"table:events_to_users"`
|
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"`
|
UserID uuid.UUID `bun:"type:uuid,pk"`
|
||||||
|
|
||||||
Event *Event `bun:"rel:belongs-to,join:event_id=event_id"`
|
Event *Event `bun:"rel:belongs-to,join:event_id=event_id"`
|
||||||
@@ -72,24 +79,24 @@ type EventToUser struct {
|
|||||||
type Blog struct {
|
type Blog struct {
|
||||||
bun.BaseModel `bun:"table:blogs"`
|
bun.BaseModel `bun:"table:blogs"`
|
||||||
|
|
||||||
BlogID uuid.UUID `bun:"type:uuid,pk"`
|
BlogID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()" json:"blogID"`
|
||||||
Slug string `bun:"slug,unique,notnull"`
|
Slug string `bun:"slug,unique,notnull" json:"slug"`
|
||||||
Content string `bun:"content,notnull"`
|
Content string `bun:"content,notnull" json:"content"`
|
||||||
Label string `bun:"label"`
|
Label string `bun:"label" json:"label"`
|
||||||
AuthorID uuid.UUID `bun:"author_id,notnull"`
|
AuthorID uuid.UUID `bun:"author_id,type:uuid,notnull" json:"authorID"`
|
||||||
Published time.Time `bun:"published,default:current_timestamp"`
|
Published time.Time `bun:"published,default:current_timestamp" json:"published"`
|
||||||
Summary string `bun:"summary"`
|
Summary string `bun:"summary" json:"summary"`
|
||||||
Image string `bun:"image"`
|
Image string `bun:"image" json:"image"`
|
||||||
Href string `bun:"href"`
|
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 {
|
type WebsiteSettings struct {
|
||||||
bun.BaseModel `bun:"table:website_settings"`
|
bun.BaseModel `bun:"table:website_settings"`
|
||||||
|
|
||||||
ID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()"`
|
ID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()" json:"id"`
|
||||||
AutoAcceptDemand bool `bun:"auto_accept_demand,default:false"`
|
AutoAcceptDemand bool `bun:"auto_accept_demand,default:false" json:"autoAcceptDemand"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitDatabase(dsn DSN) (*bun.DB, error) {
|
func InitDatabase(dsn DSN) (*bun.DB, error) {
|
||||||
|
|||||||
40
backend/api/event.go
Normal file
40
backend/api/event.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
@@ -3,50 +3,49 @@ package api
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"io"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
core "fr.latosa-escrima/api/core"
|
core "fr.latosa-escrima/api/core"
|
||||||
"github.com/uptrace/bun"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleCreateBlog(w http.ResponseWriter, r *http.Request) {
|
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 {
|
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 {
|
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{
|
core.JSONSuccess{
|
||||||
BaseModel: bun.BaseModel{},
|
Status: core.Success,
|
||||||
BlogID: [16]byte{},
|
Message: "Blog inserted",
|
||||||
Slug: "",
|
Data: blog,
|
||||||
Content: "",
|
}.Respond(w, http.StatusCreated)
|
||||||
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")
|
|
||||||
}
|
}
|
||||||
|
|||||||
51
backend/api/new_event.go
Normal file
51
backend/api/new_event.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user