Skip to content

Instantly share code, notes, and snippets.

@kasramp
Created June 9, 2020 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kasramp/6c586e63f72eb450efd885c24995ac8c to your computer and use it in GitHub Desktop.
Save kasramp/6c586e63f72eb450efd885c24995ac8c to your computer and use it in GitHub Desktop.
package com.madadipouya.quarkus.example.controller;
import com.madadipouya.quarkus.example.entity.User;
import com.madadipouya.quarkus.example.exception.UserNotFoundException;
import com.madadipouya.quarkus.example.service.UserService;
import org.jboss.resteasy.annotations.jaxrs.PathParam;
import javax.inject.Inject;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
@Path("/v1/users")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class UserController {
private final UserService userService;
@Inject
public UserController(UserService userService) {
this.userService = userService;
}
@GET
public List<User> getUsers() {
return userService.getAllUsers();
}
@GET
@Path("/{id}")
public User getUser(@PathParam("id") int id) throws UserNotFoundException {
return userService.getUserById(id);
}
@POST
public User createUser(@Valid UserDto userDto) {
return userService.saveUser(userDto.toUser());
}
@PUT
@Path("/{id}")
public User updateUser(@PathParam("id") int id, @Valid UserDto userDto) throws UserNotFoundException {
return userService.updateUser(id, userDto.toUser());
}
@DELETE
@Path("/{id}")
public Response deleteUser(@PathParam("id") int id) throws UserNotFoundException {
userService.deleteUser(id);
return Response.status(Response.Status.NO_CONTENT).build();
}
public static class UserDto {
@NotBlank
private String firstName;
@NotBlank
private String lastName;
@Min(value = 1, message = "The value must be more than 0")
@Max(value = 200, message = "The value must be less than 200")
private int age;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public User toUser() {
User user = new User();
user.setFirstName(firstName);
user.setLastName(lastName);
user.setAge(age);
return user;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment