events api file init
This commit is contained in:
36
backend/api/delete_event.go
Normal file
36
backend/api/delete_event.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
|
||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"fr.latosa-escrima/api/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
func HandleDeleteEvent(w http.ResponseWriter, r *http.Request) {
|
||||||
|
uuid := r.PathValue("event_uuid")
|
||||||
|
var event core.Event
|
||||||
|
res, err := core.DB.NewDelete().
|
||||||
|
Model(&event).
|
||||||
|
Where("id = ?", uuid).
|
||||||
|
Returning("*").
|
||||||
|
Exec(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
core.JSONError{
|
||||||
|
Status: core.Error,
|
||||||
|
Message: err.Error(),
|
||||||
|
}.Respond(w, http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Println(res)
|
||||||
|
|
||||||
|
// TODO sql request to remove content
|
||||||
|
|
||||||
|
core.JSONSuccess{
|
||||||
|
Status: core.Success,
|
||||||
|
Message: "Event deleted.",
|
||||||
|
}.Respond(w, http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
@@ -2,22 +2,16 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/http"
|
|
||||||
"io"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
core "fr.latosa-escrima/api/core"
|
core "fr.latosa-escrima/api/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleCreateEvent(w http.ResponseWriter, r *http.Request) {
|
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)
|
body, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.JSONError{
|
core.JSONError{
|
||||||
@@ -26,6 +20,7 @@ func HandleCreateEvent(w http.ResponseWriter, r *http.Request) {
|
|||||||
}.Respond(w, http.StatusNoContent)
|
}.Respond(w, http.StatusNoContent)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
log.Println(time.Now())
|
||||||
var event core.Event
|
var event core.Event
|
||||||
if err = json.Unmarshal(body, &event); err != nil {
|
if err = json.Unmarshal(body, &event); err != nil {
|
||||||
core.JSONError{
|
core.JSONError{
|
||||||
@@ -41,7 +36,7 @@ func HandleCreateEvent(w http.ResponseWriter, r *http.Request) {
|
|||||||
Status: core.Error,
|
Status: core.Error,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
}.Respond(w, http.StatusNotAcceptable)
|
}.Respond(w, http.StatusNotAcceptable)
|
||||||
}
|
}
|
||||||
|
|
||||||
core.JSONSuccess{
|
core.JSONSuccess{
|
||||||
Status: core.Success,
|
Status: core.Success,
|
||||||
|
|||||||
91
backend/api/update_event.go
Normal file
91
backend/api/update_event.go
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"fr.latosa-escrima/api/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UpdateEventArgs struct {
|
||||||
|
FirstName *string `json:"firstname,omitempty"`
|
||||||
|
LastName *string `json:"lastname,omitempty"`
|
||||||
|
Email *string `json:"email,omitempty"`
|
||||||
|
Password *string `json:"password,omitempty"`
|
||||||
|
Phone *string `json:"phone,omitempty"`
|
||||||
|
Role *core.Role `json:"role,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleUpdateEvent(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// var updateArgs UpdateEventArgs
|
||||||
|
body, err := io.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
core.JSONError{
|
||||||
|
Status: core.Error,
|
||||||
|
Message: err.Error(),
|
||||||
|
}.Respond(w, http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println("update and event here. Body : ", body)
|
||||||
|
var event core.Event
|
||||||
|
err = json.Unmarshal(body, &event)
|
||||||
|
if err != nil {
|
||||||
|
core.JSONError{
|
||||||
|
Status: core.Error,
|
||||||
|
Message: err.Error(),
|
||||||
|
}.Respond(w, http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// var event core.Event
|
||||||
|
// updateQuery := core.DB.NewUpdate().Model(&event)
|
||||||
|
//
|
||||||
|
// val := reflect.ValueOf(updateArgs)
|
||||||
|
// typ := reflect.TypeOf(updateArgs)
|
||||||
|
//
|
||||||
|
// for i := 0; i < val.NumField(); i++ {
|
||||||
|
// field := val.Field(i)
|
||||||
|
// fieldname := typ.Field(i).Name
|
||||||
|
//
|
||||||
|
// tag := typ.Field(i).Tag.Get("bun")
|
||||||
|
// if tag == "" {
|
||||||
|
// tag = typ.Field(i).Tag.Get("json")
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // Only add fields that are non-nil and non-zero
|
||||||
|
// if field.IsValid() && !field.IsNil() && !field.IsZero() {
|
||||||
|
// if fieldname == "Password" {
|
||||||
|
// updateQuery.Set(fmt.Sprintf("%s = crypt(?, gen_salt('bf'))", strings.Split(tag, ",")[0]), field.Interface())
|
||||||
|
// } else {
|
||||||
|
// updateQuery.Set(fmt.Sprintf("%s = ?", strings.Split(tag, ",")[0]), field.Interface())
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // Always update the `updated_at` field
|
||||||
|
// updateQuery.Set("updated_at = ?", time.Now())
|
||||||
|
//
|
||||||
|
// uuid := r.PathValue("user_uuid")
|
||||||
|
// _, err = updateQuery.
|
||||||
|
// Where("user_id = ?", uuid).
|
||||||
|
// Returning("*").
|
||||||
|
// Exec(context.Background())
|
||||||
|
//
|
||||||
|
// if err != nil {
|
||||||
|
// core.JSONError{
|
||||||
|
// Status: core.Error,
|
||||||
|
// Message: err.Error(),
|
||||||
|
// }.Respond(w, http.StatusInternalServerError)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// user.Password = ""
|
||||||
|
//
|
||||||
|
core.JSONSuccess{
|
||||||
|
Status: core.Success,
|
||||||
|
Message: "User updated.",
|
||||||
|
Data: "ok",
|
||||||
|
}.Respond(w, http.StatusOK)
|
||||||
|
}
|
||||||
@@ -105,11 +105,24 @@ func main() {
|
|||||||
"/users/{user_uuid}/update": {
|
"/users/{user_uuid}/update": {
|
||||||
Handler: api.HandleUpdateUser,
|
Handler: api.HandleUpdateUser,
|
||||||
Middlewares: []core.Middleware{api.Methods("PATCH"), api.AuthJWT}},
|
Middlewares: []core.Middleware{api.Methods("PATCH"), api.AuthJWT}},
|
||||||
// "/users/{user_uuid}/events": {Handler: nil, Middleware: nil},
|
"/events": {
|
||||||
// "/users/{user_uuid}/events/{event_uuid}": {Handler: nil, Middleware: nil},
|
Handler: api.HandleGetEvent,
|
||||||
// "/users/{user_uuid}/events/{event_uuid}/delete": {Handler: nil, Middleware: nil},
|
Middlewares: []core.Middleware{api.Methods("GET")}},
|
||||||
// "/users/{user_uuid}/events/{event_uuid}/update": {Handler: nil, Middleware: nil},
|
"/events/new": {
|
||||||
"/blogs/new": {Handler: api.HandleCreateBlog, Middlewares: nil},
|
Handler: api.HandleCreateEvent,
|
||||||
|
Middlewares: []core.Middleware{api.Methods("POST")}},
|
||||||
|
"/events/{event_uuid}": {
|
||||||
|
Handler: api.HandleGetEvent,
|
||||||
|
Middlewares: []core.Middleware{api.Methods("GET")}},
|
||||||
|
"/events/{event_uuid}/delete": {
|
||||||
|
Handler: api.HandleDeleteEvent,
|
||||||
|
Middlewares: []core.Middleware{api.Methods("DELETE")}},
|
||||||
|
"/events/{event_uuid}/update": {
|
||||||
|
Handler: api.HandleUpdateEvent,
|
||||||
|
Middlewares: []core.Middleware{api.Methods("PATCH")}},
|
||||||
|
"/blogs/new": {
|
||||||
|
Handler: api.HandleCreateBlog,
|
||||||
|
Middlewares: []core.Middleware{api.Methods(("POST"))}},
|
||||||
"/blogs/{uuid}": {
|
"/blogs/{uuid}": {
|
||||||
Handler: api.HandleGetBlog,
|
Handler: api.HandleGetBlog,
|
||||||
Middlewares: []core.Middleware{api.Methods("GET")}},
|
Middlewares: []core.Middleware{api.Methods("GET")}},
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
|
import { request } from "@/hooks/use-api";
|
||||||
import "@schedule-x/theme-shadcn/dist/index.css";
|
import "@schedule-x/theme-shadcn/dist/index.css";
|
||||||
import { useNextCalendarApp, ScheduleXCalendar } from "@schedule-x/react";
|
import { useNextCalendarApp, ScheduleXCalendar } from "@schedule-x/react";
|
||||||
import { createEventsServicePlugin } from "@schedule-x/events-service";
|
import { createEventsServicePlugin } from "@schedule-x/events-service";
|
||||||
@@ -36,7 +37,7 @@ const Planning = () => {
|
|||||||
useState<CalendarEventExternal | null>(null);
|
useState<CalendarEventExternal | null>(null);
|
||||||
const [events, setEvents] = useState<CalendarEventExternal[]>([
|
const [events, setEvents] = useState<CalendarEventExternal[]>([
|
||||||
{
|
{
|
||||||
id: "1",
|
id: "1", // TODO put an uuid there
|
||||||
title: "Event 1",
|
title: "Event 1",
|
||||||
start: format(new Date(Date.now()), "yyyy-MM-dd HH:mm"),
|
start: format(new Date(Date.now()), "yyyy-MM-dd HH:mm"),
|
||||||
end: format(
|
end: format(
|
||||||
@@ -45,6 +46,7 @@ const Planning = () => {
|
|||||||
),
|
),
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const calendar = useNextCalendarApp(
|
const calendar = useNextCalendarApp(
|
||||||
{
|
{
|
||||||
theme: "shadcn",
|
theme: "shadcn",
|
||||||
@@ -61,7 +63,7 @@ const Planning = () => {
|
|||||||
callbacks: {
|
callbacks: {
|
||||||
onEventClick(event, e) {
|
onEventClick(event, e) {
|
||||||
setEventSelected(event);
|
setEventSelected(event);
|
||||||
},
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
plugins,
|
plugins,
|
||||||
@@ -78,7 +80,7 @@ const Planning = () => {
|
|||||||
<ScheduleXCalendar calendarApp={calendar} />
|
<ScheduleXCalendar calendarApp={calendar} />
|
||||||
</div>
|
</div>
|
||||||
<Dialog
|
<Dialog
|
||||||
open={eventSelected !== null}
|
open={eventSelected !== null || false}
|
||||||
onOpenChange={(open) => {
|
onOpenChange={(open) => {
|
||||||
setEventSelected((e) => (open ? e : null));
|
setEventSelected((e) => (open ? e : null));
|
||||||
}}
|
}}
|
||||||
@@ -137,10 +139,9 @@ const Planning = () => {
|
|||||||
</Popover> */}
|
</Popover> */}
|
||||||
<Input
|
<Input
|
||||||
id="start"
|
id="start"
|
||||||
value={eventSelected?.start}
|
value={eventSelected?.start || ""}
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
const val = e.currentTarget.value;
|
const val = e.currentTarget.value;
|
||||||
console.log(val);
|
|
||||||
setEventSelected((ev) => {
|
setEventSelected((ev) => {
|
||||||
if (ev)
|
if (ev)
|
||||||
return {
|
return {
|
||||||
@@ -159,36 +160,48 @@ const Planning = () => {
|
|||||||
</Label>
|
</Label>
|
||||||
<Input
|
<Input
|
||||||
id="end"
|
id="end"
|
||||||
value={eventSelected?.end}
|
value={eventSelected?.end || ""}
|
||||||
onChange={(e) =>
|
onChange={(e) => {
|
||||||
|
const val = e.currentTarget.value
|
||||||
setEventSelected((ev) => {
|
setEventSelected((ev) => {
|
||||||
if (ev)
|
if (ev)
|
||||||
return {
|
return {
|
||||||
...ev,
|
...ev,
|
||||||
end: e.currentTarget.value,
|
end: val,
|
||||||
};
|
};
|
||||||
return ev;
|
return ev;
|
||||||
})
|
})
|
||||||
}
|
}}
|
||||||
className="col-span-3"
|
className="col-span-3"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<DialogFooter>
|
<DialogFooter> <Button
|
||||||
<Button
|
onClick={async () => {
|
||||||
onClick={() => {
|
calendar?.events?.update(eventSelected!)
|
||||||
setEvents((evs) => {
|
try {
|
||||||
evs = evs.filter(
|
const res = await request<undefined>(
|
||||||
(e) => e.id !== eventSelected?.id,
|
`/events/${eventSelected!.id}/update`,
|
||||||
);
|
{
|
||||||
evs.push(eventSelected!);
|
method: "PATCH",
|
||||||
return evs;
|
body: JSON.stringify(eventSelected),
|
||||||
});
|
requiresAuth: true,
|
||||||
}}
|
csrfToken: false
|
||||||
type="submit"
|
},)
|
||||||
>
|
if (res.status === "Error") {
|
||||||
Mettre à jour
|
console.log("Error")
|
||||||
</Button>
|
}
|
||||||
|
if (res.status === "Success") {
|
||||||
|
console.log("Success")
|
||||||
|
}
|
||||||
|
} catch(e) {
|
||||||
|
console.log(e)
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
type="submit"
|
||||||
|
>
|
||||||
|
Mettre à jour
|
||||||
|
</Button>
|
||||||
</DialogFooter>
|
</DialogFooter>
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
|
|||||||
38
frontend/hooks/events.tsx
Normal file
38
frontend/hooks/events.tsx
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { setCookie } from "cookies-next";
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { API_URL } from "@/lib/constants";
|
||||||
|
|
||||||
|
export interface LoginArgs {
|
||||||
|
email: string;
|
||||||
|
password: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function useLogin() {
|
||||||
|
const {
|
||||||
|
data,
|
||||||
|
isLoading: loading,
|
||||||
|
isSuccess,
|
||||||
|
} = request<string, LoginArgs>(
|
||||||
|
"/users/login",
|
||||||
|
undefined,
|
||||||
|
"POST",
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
|
||||||
|
const login = async (inputs: LoginArgs) => {
|
||||||
|
try {
|
||||||
|
const res = await trigger(inputs);
|
||||||
|
if (!res) throw new Error("The server hasn't responded.");
|
||||||
|
if (res.status === "Error") throw new Error(res.message);
|
||||||
|
if (res.data) setCookie("auth_token", res.data);
|
||||||
|
return res;
|
||||||
|
} catch (error: any) {
|
||||||
|
throw new Error(error.message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return { login, loading, isSuccess };
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user