88 lines
2.7 KiB
Java
88 lines
2.7 KiB
Java
package com.valposystems.resource;
|
|
|
|
import com.valposystems.dto.GymClassDTO;
|
|
import com.valposystems.service.GymClassService;
|
|
import jakarta.enterprise.context.ApplicationScoped;
|
|
import jakarta.inject.Inject;
|
|
import jakarta.transaction.Transactional;
|
|
import jakarta.ws.rs.*;
|
|
import jakarta.ws.rs.core.MediaType;
|
|
import jakarta.ws.rs.core.Response;
|
|
import java.util.List;
|
|
|
|
@ApplicationScoped
|
|
@Path("/api/classes")
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
public class GymClassResource {
|
|
|
|
@Inject
|
|
GymClassService gymClassService;
|
|
|
|
@GET
|
|
@Transactional
|
|
public Response getAllClasses() {
|
|
try {
|
|
List<GymClassDTO> classes = gymClassService.getAllClasses();
|
|
return Response.ok(classes).build();
|
|
} catch (Exception e) {
|
|
return Response.serverError().entity("Error: " + e.getMessage()).build();
|
|
}
|
|
}
|
|
|
|
@GET
|
|
@Path("/{id}")
|
|
@Transactional
|
|
public Response getClassById(@PathParam("id") Long id) {
|
|
try {
|
|
GymClassDTO gymClass = gymClassService.getClassById(id);
|
|
if (gymClass == null) {
|
|
return Response.status(Response.Status.NOT_FOUND).build();
|
|
}
|
|
return Response.ok(gymClass).build();
|
|
} catch (Exception e) {
|
|
return Response.serverError().entity("Error: " + e.getMessage()).build();
|
|
}
|
|
}
|
|
|
|
@POST
|
|
@Transactional
|
|
public Response createClass(GymClassDTO dto) {
|
|
try {
|
|
GymClassDTO created = gymClassService.createClass(dto);
|
|
return Response.status(Response.Status.CREATED).entity(created).build();
|
|
} catch (Exception e) {
|
|
return Response.serverError().entity("Error: " + e.getMessage()).build();
|
|
}
|
|
}
|
|
|
|
@PUT
|
|
@Path("/{id}")
|
|
@Transactional
|
|
public Response updateClass(@PathParam("id") Long id, GymClassDTO dto) {
|
|
try {
|
|
GymClassDTO updated = gymClassService.updateClass(id, dto);
|
|
if (updated == null) {
|
|
return Response.status(Response.Status.NOT_FOUND).build();
|
|
}
|
|
return Response.ok(updated).build();
|
|
} catch (Exception e) {
|
|
return Response.serverError().entity("Error: " + e.getMessage()).build();
|
|
}
|
|
}
|
|
|
|
@DELETE
|
|
@Path("/{id}")
|
|
@Transactional
|
|
public Response deleteClass(@PathParam("id") Long id) {
|
|
try {
|
|
boolean deleted = gymClassService.deleteClass(id);
|
|
if (!deleted) {
|
|
return Response.status(Response.Status.NOT_FOUND).build();
|
|
}
|
|
return Response.noContent().build();
|
|
} catch (Exception e) {
|
|
return Response.serverError().entity("Error: " + e.getMessage()).build();
|
|
}
|
|
}
|
|
} |