From 21c5ce25f2eba8d5ae7feffacf867fac2cbcb5c8 Mon Sep 17 00:00:00 2001 From: elleguehen Date: Fri, 26 May 2023 12:00:03 +0200 Subject: [PATCH] =?UTF-8?q?Avanc=C3=A9es=20sur=20l'api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flagg10ma/taf/dto/EventFromListDto.java | 23 ++++++++ .../src/com/flagg10ma/taf/dto/LabelDto.java | 11 ++++ .../src/com/flagg10ma/taf/dto/LoginDto.java | 3 + .../com/flagg10ma/taf/dto/NewEventDto.java | 22 +++++++ .../com/flagg10ma/taf/dto/NewLabelDto.java | 10 ++++ .../src/com/flagg10ma/taf/dto/NewUserDto.java | 3 + .../src/com/flagg10ma/taf/dto/UserDto.java | 10 ++++ .../flagg10ma/taf/resource/EventResource.java | 50 ++++++++++++++++ .../flagg10ma/taf/resource/LabelResource.java | 44 ++++++++++++++ .../flagg10ma/taf/resource/TaskResource.java | 49 ++++++++++++++++ .../flagg10ma/taf/resource/UserResource.java | 57 +++++++++++++++++++ 11 files changed, 282 insertions(+) create mode 100644 src/back/src/com/flagg10ma/taf/dto/EventFromListDto.java create mode 100644 src/back/src/com/flagg10ma/taf/dto/LabelDto.java create mode 100644 src/back/src/com/flagg10ma/taf/dto/LoginDto.java create mode 100644 src/back/src/com/flagg10ma/taf/dto/NewEventDto.java create mode 100644 src/back/src/com/flagg10ma/taf/dto/NewLabelDto.java create mode 100644 src/back/src/com/flagg10ma/taf/dto/NewUserDto.java create mode 100644 src/back/src/com/flagg10ma/taf/dto/UserDto.java create mode 100644 src/back/src/com/flagg10ma/taf/resource/EventResource.java create mode 100644 src/back/src/com/flagg10ma/taf/resource/LabelResource.java create mode 100644 src/back/src/com/flagg10ma/taf/resource/TaskResource.java create mode 100644 src/back/src/com/flagg10ma/taf/resource/UserResource.java diff --git a/src/back/src/com/flagg10ma/taf/dto/EventFromListDto.java b/src/back/src/com/flagg10ma/taf/dto/EventFromListDto.java new file mode 100644 index 0000000..3e5d151 --- /dev/null +++ b/src/back/src/com/flagg10ma/taf/dto/EventFromListDto.java @@ -0,0 +1,23 @@ +package com.flagg10ma.taf.dto; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.time.LocalDate; + +public record EventFromListDto( + @JsonProperty("event_id") String id, + String title, + String description, + + @JsonProperty("start_date") + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy") + LocalDate startDate, + + @JsonProperty("end_date") + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy") + LocalDate endDate, + + @JsonProperty("color_code") String color +) { +} diff --git a/src/back/src/com/flagg10ma/taf/dto/LabelDto.java b/src/back/src/com/flagg10ma/taf/dto/LabelDto.java new file mode 100644 index 0000000..e8b881e --- /dev/null +++ b/src/back/src/com/flagg10ma/taf/dto/LabelDto.java @@ -0,0 +1,11 @@ +package com.flagg10ma.taf.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public record LabelDto( + @JsonProperty String id, + String title, + String description, + @JsonProperty("color_code") String color +) { +} diff --git a/src/back/src/com/flagg10ma/taf/dto/LoginDto.java b/src/back/src/com/flagg10ma/taf/dto/LoginDto.java new file mode 100644 index 0000000..385c29a --- /dev/null +++ b/src/back/src/com/flagg10ma/taf/dto/LoginDto.java @@ -0,0 +1,3 @@ +package com.flagg10ma.taf.dto; + +public record LoginDto(String login, String password){} diff --git a/src/back/src/com/flagg10ma/taf/dto/NewEventDto.java b/src/back/src/com/flagg10ma/taf/dto/NewEventDto.java new file mode 100644 index 0000000..eab1009 --- /dev/null +++ b/src/back/src/com/flagg10ma/taf/dto/NewEventDto.java @@ -0,0 +1,22 @@ +package com.flagg10ma.taf.dto; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.time.LocalDate; + +public record NewEventDto( + String title, + String description, + + @JsonProperty("start_date") + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy") + LocalDate startDate, + + @JsonProperty("end_date") + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy") + LocalDate endDate, + + @JsonProperty("color_code") String color +) { +} diff --git a/src/back/src/com/flagg10ma/taf/dto/NewLabelDto.java b/src/back/src/com/flagg10ma/taf/dto/NewLabelDto.java new file mode 100644 index 0000000..a43b699 --- /dev/null +++ b/src/back/src/com/flagg10ma/taf/dto/NewLabelDto.java @@ -0,0 +1,10 @@ +package com.flagg10ma.taf.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public record NewLabelDto( + String title, + String description, + @JsonProperty("color_code") String color +) { +} diff --git a/src/back/src/com/flagg10ma/taf/dto/NewUserDto.java b/src/back/src/com/flagg10ma/taf/dto/NewUserDto.java new file mode 100644 index 0000000..d6b3343 --- /dev/null +++ b/src/back/src/com/flagg10ma/taf/dto/NewUserDto.java @@ -0,0 +1,3 @@ +package com.flagg10ma.taf.dto; + +public record NewUserDto(String login, String password){} diff --git a/src/back/src/com/flagg10ma/taf/dto/UserDto.java b/src/back/src/com/flagg10ma/taf/dto/UserDto.java new file mode 100644 index 0000000..4dc9175 --- /dev/null +++ b/src/back/src/com/flagg10ma/taf/dto/UserDto.java @@ -0,0 +1,10 @@ +package com.flagg10ma.taf.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public record UserDto( + @JsonProperty String id, + String login, + String password +) { +} diff --git a/src/back/src/com/flagg10ma/taf/resource/EventResource.java b/src/back/src/com/flagg10ma/taf/resource/EventResource.java new file mode 100644 index 0000000..f448be5 --- /dev/null +++ b/src/back/src/com/flagg10ma/taf/resource/EventResource.java @@ -0,0 +1,50 @@ +package com.flagg10ma.taf.resource; + +import com.flagg10ma.taf.model.Event; + +import javax.inject.Inject; +import javax.transaction.Transactional; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +@Path("/api/events") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class EventResource { + + @Inject + //EventService eventService; + + @GET + public Response getAllEvents() { + // Retrieve and return a list of events + } + + @GET + @Path("/{id}") + public Response getEventById(@PathParam("id") Long id) { + // Retrieve and return a single event by its ID + } + + @POST + @Transactional + public Response createEvent(Event event) { + // Create a new event + } + + @PUT + @Path("/{id}") + @Transactional + public Response updateEvent(@PathParam("id") Long id, Event event) { + // Update an existing event by its ID + } + + @DELETE + @Path("/{id}") + @Transactional + public Response deleteEvent(@PathParam("id") Long id) { + // Delete an event by its ID + } +} + diff --git a/src/back/src/com/flagg10ma/taf/resource/LabelResource.java b/src/back/src/com/flagg10ma/taf/resource/LabelResource.java new file mode 100644 index 0000000..86a96ce --- /dev/null +++ b/src/back/src/com/flagg10ma/taf/resource/LabelResource.java @@ -0,0 +1,44 @@ +package com.flagg10ma.taf.resource; + +import com.flagg10ma.taf.model.Label; + +import javax.inject.Inject; +import javax.transaction.Transactional; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +@Path("/api/labels") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class LabelResource { + + @Inject + //LabelService labelService; + + @GET + public Response getAllLabels() { + // Retrieve and return a list of labels + } + + @POST + @Transactional + public Response createLabel(Label label) { + // Create a new label + } + + @PUT + @Path("/{id}") + @Transactional + public Response updateLabel(@PathParam("id") Long id, Label label) { + // Update an existing label by its ID + } + + @DELETE + @Path("/{id}") + @Transactional + public Response deleteLabel(@PathParam("id") Long id) { + // Delete a label by its ID + } +} + diff --git a/src/back/src/com/flagg10ma/taf/resource/TaskResource.java b/src/back/src/com/flagg10ma/taf/resource/TaskResource.java new file mode 100644 index 0000000..9234112 --- /dev/null +++ b/src/back/src/com/flagg10ma/taf/resource/TaskResource.java @@ -0,0 +1,49 @@ +package com.flagg10ma.taf.resource; + +import com.flagg10ma.taf.model.Task; + +import javax.inject.Inject; +import javax.transaction.Transactional; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +@Path("/api/tasks") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class TaskResource { + + @Inject + //TaskService taskService; + + @GET + public Response getAllTasks() { + // Retrieve and return a list of tasks + } + + @GET + @Path("/{id}") + public Response getTaskById(@PathParam("id") String id) { + // Retrieve and return a single task by its ID + } + + @POST + @Transactional + public Response createTask(Task task) { + // Create a new task + } + + @PUT + @Path("/{id}") + @Transactional + public Response updateTask(@PathParam("id") String id, Task task) { + // Update an existing task by its ID + } + + @DELETE + @Path("/{id}") + @Transactional + public Response deleteTask(@PathParam("id") Long id) { + // Delete a task by its ID + } +} diff --git a/src/back/src/com/flagg10ma/taf/resource/UserResource.java b/src/back/src/com/flagg10ma/taf/resource/UserResource.java new file mode 100644 index 0000000..c031fbf --- /dev/null +++ b/src/back/src/com/flagg10ma/taf/resource/UserResource.java @@ -0,0 +1,57 @@ +package com.flagg10ma.taf.resource; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.flagg10ma.taf.model.User; + +import javax.inject.Inject; +import javax.transaction.Transactional; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriBuilder; +import java.util.Collections; + +@Path("/api/users") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class UserResource { + + @Inject + //UserService userService; + + @POST + public Response createUser(JsonNode requestBody) { + try { + ObjectMapper objectMapper = new ObjectMapper(); + String login = requestBody.get("login").asText(); + String password = requestBody.get("password").asText(); + + // Create a new user + User newUser = new User(null, login, password, Collections.emptyList()); + //userService.createUser(newUser); + + // Create the response object with createdAt and userId + //UserResponse userResponse = new UserResponse(newUser.getId(), newUser.getCreatedAt()); + + return Response.created(UriBuilder.fromPath("/api/users/{id}").build(newUser.id())).build(); + } catch (Exception e) { + // Handle any exceptions or validation errors + return Response.status(Response.Status.BAD_REQUEST).entity("Invalid request body").build(); + } + } + + @PUT + @Path("/{id}") + @Transactional + public Response updateUser(@PathParam("id") Long id, User user) { + // Update an existing user by ID + } + + @DELETE + @Path("/{id}") + @Transactional + public Response deleteUser(@PathParam("id") Long id) { + // Delete a user by ID + } +}