diff --git a/backend/api/get_event.go b/backend/api/get_event.go index dc50e4b..56bbc11 100644 --- a/backend/api/get_event.go +++ b/backend/api/get_event.go @@ -2,6 +2,7 @@ package api import ( "context" + "fmt" "net/http" core "fr.latosa-escrima/api/core" @@ -9,20 +10,40 @@ import ( func HandleGetEvent(w http.ResponseWriter, r *http.Request) { event_uuid := r.PathValue("event_uuid") - var event core.Event + 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, + Status: core.Error, Message: err.Error(), }.Respond(w, http.StatusInternalServerError) - return + return + } + + core.JSONSuccess{ + Status: core.Success, + Message: "Event successfully sent", + Data: event, + }.Respond(w, http.StatusOK) + return +} + +func HangleGetEvents(w http.ResponseWriter, r *http.Request) { + var events []core.Event + rowsCount, err := core.DB.NewSelect().Model(&events).ScanAndCount(context.Background()) + if err != nil { + core.JSONError{ + Status: core.Error, + Message: err.Error(), + }.Respond(w, http.StatusInternalServerError) + return } core.JSONSuccess{ Status: core.Success, - Message: "Event successfully sent", - Data: event, + Message: fmt.Sprintf("%d Event successfully sent", rowsCount), + Data: events, }.Respond(w, http.StatusOK) - return + return } + diff --git a/backend/main.go b/backend/main.go index ddea390..7e03dfe 100644 --- a/backend/main.go +++ b/backend/main.go @@ -106,22 +106,22 @@ func main() { Handler: api.HandleUpdateUser, Middlewares: []core.Middleware{api.Methods("PATCH"), api.AuthJWT}}, "/events": { - Handler: api.HandleGetEvent, + Handler: api.HangleGetEvents, Middlewares: []core.Middleware{api.Methods("GET")}}, "/events/new": { - Handler: api.HandleCreateEvent, + Handler: api.HandleCreateEvent, Middlewares: []core.Middleware{api.Methods("POST")}}, "/events/{event_uuid}": { - Handler: api.HandleGetEvent, + Handler: api.HandleGetEvent, Middlewares: []core.Middleware{api.Methods("GET")}}, "/events/{event_uuid}/delete": { - Handler: api.HandleDeleteEvent, + Handler: api.HandleDeleteEvent, Middlewares: []core.Middleware{api.Methods("DELETE")}}, "/events/{event_uuid}/update": { - Handler: api.HandleUpdateEvent, + Handler: api.HandleUpdateEvent, Middlewares: []core.Middleware{api.Methods("PATCH")}}, "/blogs/new": { - Handler: api.HandleCreateBlog, + Handler: api.HandleCreateBlog, Middlewares: []core.Middleware{api.Methods(("POST"))}}, "/blogs/{uuid}": { Handler: api.HandleGetBlog, diff --git a/frontend/app/(main)/planning/page.tsx b/frontend/app/(main)/planning/page.tsx index dbe47c7..3584f6a 100644 --- a/frontend/app/(main)/planning/page.tsx +++ b/frontend/app/(main)/planning/page.tsx @@ -1,6 +1,6 @@ "use client"; -import { request } from "@/hooks/use-api"; +import { request, useApi } from "@/hooks/use-api"; import "@schedule-x/theme-shadcn/dist/index.css"; import { useNextCalendarApp, ScheduleXCalendar } from "@schedule-x/react"; import { createEventsServicePlugin } from "@schedule-x/events-service"; @@ -30,6 +30,7 @@ import { import { CalendarIcon } from "lucide-react"; import { Calendar } from "@/components/ui/calendar"; import { cn } from "@/lib/utils"; +import { requestFormReset } from "react-dom"; const Planning = () => { const plugins = [createEventsServicePlugin()]; @@ -47,6 +48,8 @@ const Planning = () => { }, ]); + const [requestCreateEvent, setRequestCreateEvent] = useState(false) + const calendar = useNextCalendarApp( { theme: "shadcn", @@ -63,12 +66,48 @@ const Planning = () => { callbacks: { onEventClick(event, e) { setEventSelected(event); - } + }, + async onClickDateTime(dateTime) { + setRequestCreateEvent(true) + const newEvent: CalendarEventExternal = { + id: "5", + title: "Event 1", + start: dateTime, + end: format( + new Date(dateTime).getTime() + (1000 * 60 * 60), + "yyyy-MM-dd HH:mm", + ), + } + try { + const res = await request( + `/events/new`, + { + method: "POST", + body: JSON.stringify(newEvent), + requiresAuth: true, + csrfToken: false + },) + if (res.status === "Error") { + console.log("Error") + } + if (res.status === "Success") { + console.log("Success") + } + } catch (e) { + console.log(e) + } + }, }, }, plugins, ); - + + const {data: requestedEvents, isLoading, success} = useApi("/events", { + onSuccess: (data) => { + calendar?.events.set(data) + } + }, false, false) + useEffect(() => { // get all events calendar?.events.getAll(); @@ -176,32 +215,57 @@ const Planning = () => { /> - + }} + type="submit"> + Supprimer + +