diff --git a/core/src/main/java/cz/muni/fi/pa165/core/common/DomainMapper.java b/core/src/main/java/cz/muni/fi/pa165/core/common/DomainMapper.java index a770ec923a74135116c61082d16bf26e406f9ab4..358c511df622eee2f475320fcb09f43467b3936d 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/common/DomainMapper.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/common/DomainMapper.java @@ -1,6 +1,10 @@ package cz.muni.fi.pa165.core.common; import cz.muni.fi.pa165.model.dto.common.DomainObjectDto; +import cz.muni.fi.pa165.model.dto.common.Result; +import org.mapstruct.Mapping; +import org.mapstruct.Mappings; +import org.springframework.data.domain.Page; import java.util.List; @@ -12,7 +16,6 @@ public interface DomainMapper<T extends DomainObject, S extends DomainObjectDto> List<S> toDtoList(List<T> entities); - /* @Mappings({ @Mapping(target = "total", expression = "java(source.getTotalElements())"), @Mapping(target = "page", expression = "java(source.getNumber())"), @@ -20,5 +23,5 @@ public interface DomainMapper<T extends DomainObject, S extends DomainObjectDto> @Mapping(target = "items", expression = "java(toDtoList(source.getContent()))") }) Result<S> toResult(Page<T> source); - */ + } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/user/UserController.java b/core/src/main/java/cz/muni/fi/pa165/core/user/UserController.java index ca8bbd0592d3778c8ab4f77b229bf24a0088ab73..a7c5b01902fa40d353e50e6005cb3c1354363f6c 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/user/UserController.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/user/UserController.java @@ -1,19 +1,19 @@ package cz.muni.fi.pa165.core.user; +import cz.muni.fi.pa165.model.dto.common.Result; import cz.muni.fi.pa165.model.dto.user.UserCreateDto; import cz.muni.fi.pa165.model.dto.user.UserDto; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import java.util.List; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import org.springframework.web.bind.annotation.*; @RestController -@RequestMapping("/users") +@RequestMapping("/api/user") public class UserController { private final UserService service; @@ -26,23 +26,95 @@ public class UserController { this.mapper = mapper; } - @GetMapping - public List<UserDto> findAll() { - return service.findAll().stream().map(mapper::toDto).toList(); - } - + @Operation( + summary = "Find user by ID", + description = "Returns a single user", + tags = {"user"}) + @ApiResponses( + value = { + @ApiResponse( + responseCode = "200", + description = "User found", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = UserDto.class)) + }), + @ApiResponse(responseCode = "404", description = "User not found", content = @Content) + }) @GetMapping("/{id}") - public UserDto find(@PathVariable String id) { + public UserDto find( + @Parameter(description = "ID of user to be searched") @PathVariable String id) { return mapper.toDto(service.find(id)); } + @Operation( + summary = "Create user", + description = "Creates a new user", + tags = {"user"}) + @ApiResponses( + value = { + @ApiResponse( + responseCode = "201", + description = "User created", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = UserDto.class)) + }), + @ApiResponse(responseCode = "400", description = "Invalid input", content = @Content), + @ApiResponse( + responseCode = "409", + description = "User with the same name already exists", + content = @Content) + }) @PostMapping - public UserDto create(@RequestBody UserCreateDto dto) { + public UserDto create( + @Parameter(description = "User to be created") @RequestBody UserCreateDto dto) { return mapper.toDto(service.create(mapper.fromCreateDto(dto))); } - // @GetMapping - // public Result<UserDto> findAll(@RequestParam int page) { - // return mapper.toResult(service.findAll(page)); - // } + @Operation( + summary = "Get all users", + description = "Returns all users", + tags = {"user"}) + @ApiResponses( + value = { + @ApiResponse( + responseCode = "200", + description = "Users found", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = Result.class)) + }) + }) + @GetMapping + public Result<UserDto> findAll( + @Parameter(description = "Page number of results to retrieve") @RequestParam int page) { + return mapper.toResult(service.findAllPageableInt(page)); + } + + @Operation( + summary = "Update user", + description = "Updates an existing user", + tags = {"user"}) + @ApiResponses( + value = { + @ApiResponse( + responseCode = "200", + description = "User updated", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = UserDto.class)) + }), + @ApiResponse(responseCode = "400", description = "Invalid input", content = @Content), + @ApiResponse(responseCode = "404", description = "User not found", content = @Content) + }) + @PutMapping + public UserDto update( + @Parameter(description = "User to be updated") @RequestBody UserCreateDto userCreateDto) { + return mapper.toDto(service.create(mapper.fromUpdateDto(userCreateDto))); + } } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/user/UserMapper.java b/core/src/main/java/cz/muni/fi/pa165/core/user/UserMapper.java index 32642e778cd87aba705828f27b774fed6be972ce..6a837566714649d1baefe229dd3826c65ff551b5 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/user/UserMapper.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/user/UserMapper.java @@ -1,12 +1,19 @@ package cz.muni.fi.pa165.core.user; import cz.muni.fi.pa165.core.common.DomainMapper; +import cz.muni.fi.pa165.core.device.Device; +import cz.muni.fi.pa165.model.dto.common.Result; +import cz.muni.fi.pa165.model.dto.device.DeviceUpdateDto; import cz.muni.fi.pa165.model.dto.user.UserCreateDto; import cz.muni.fi.pa165.model.dto.user.UserDto; import org.mapstruct.Mapper; +import org.springframework.data.domain.Page; @Mapper public interface UserMapper extends DomainMapper<User, UserDto> { User fromCreateDto(UserCreateDto dto); + + User fromUpdateDto(UserCreateDto dto); + } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/user/UserService.java b/core/src/main/java/cz/muni/fi/pa165/core/user/UserService.java index c1ff6359b20ddba9a6b35a8e7f4af38fa34c5639..c6982540f2a0d5fbccad34b2cc853c1d43be7e9b 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/user/UserService.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/user/UserService.java @@ -27,7 +27,7 @@ public class UserService extends DomainService<User> { } @Transactional(readOnly = true) - public List<User> findAll() { - return repository.findAll(); + public void deleteAll() { + repository.deleteAll(); } } diff --git a/core/src/test/java/cz/muni/fi/pa165/core/user/UserControllerTest.java b/core/src/test/java/cz/muni/fi/pa165/core/user/UserControllerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..3017240128bda4ac949b6b8ce49ea6e367607f32 --- /dev/null +++ b/core/src/test/java/cz/muni/fi/pa165/core/user/UserControllerTest.java @@ -0,0 +1,79 @@ +package cz.muni.fi.pa165.core.user; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.web.servlet.MockMvc; + +import java.util.List; + +@WebMvcTest(controllers = UserController.class) +public class UserControllerTest { + + @Autowired private MockMvc mockMvc; + + @Autowired private ObjectMapper objectMapper; + @MockBean private UserService service; + + @BeforeEach + public void setUp() { + List.of( + new User() + .builder() + .firstName("Marek") + .lastName("Pavel") + .email("marek@gmail.cz") + .userType(UserType.NORMAL) + .password("adminadmin") + .build(), + new User() + .builder() + .firstName("John") + .lastName("Doe") + .email("john.doe@gmail.com") + .userType(UserType.NORMAL) + .password("password123") + .build(), + new User() + .builder() + .firstName("Jane") + .lastName("Doe") + .email("jane.doe@gmail.com") + .userType(UserType.ADMIN) + .password("qwertyuiop") + .build()) + .stream() + .forEach(service::create); + } + + @AfterEach + public void dropData() { + service.deleteAll(); + } + /* + @Test + void getUserTest() throws Exception { + for (User user : service.findAll()) { + mockMvc.perform(get("/api/user/" + user.getId())).andExpect(status().isOk()); + + + .andExpect(content().json("{\"id\":\"" + user.getId() + + "\",\"username\":\"" + + user.getUsername() + + "\",\"email\":\"" + + user.getEmail() + + "\",\"firstName\":\"" + + user.getFirstName() + + "\",\"lastName\":\"" + + user.getLastName() + + "\"}")); + * + } + + } + */ +} diff --git a/model/src/main/java/cz/muni/fi/pa165/model/dto/common/Result.java b/model/src/main/java/cz/muni/fi/pa165/model/dto/common/Result.java new file mode 100644 index 0000000000000000000000000000000000000000..9eb9582e9247bb9e193bc5f383b4438c79282f5f --- /dev/null +++ b/model/src/main/java/cz/muni/fi/pa165/model/dto/common/Result.java @@ -0,0 +1,16 @@ +package cz.muni.fi.pa165.model.dto.common; + +import lombok.Getter; +import lombok.Setter; + +import java.util.List; + +@Getter +@Setter +public class Result<T extends DomainObjectDto> { + + private long total; + private int page; + private int pageSize; + private List<T> items; +} diff --git a/pom.xml b/pom.xml index 94bc7a50e4efcf73eb22e0321bdabccf9e87de7d..9013c36637e5284e9d14b158329414d4d8c2c0b4 100644 --- a/pom.xml +++ b/pom.xml @@ -91,28 +91,6 @@ </compilerArgs> </configuration> </plugin> - <plugin> - <groupId>com.theoryinpractise</groupId> - <artifactId>googleformatter-maven-plugin</artifactId> - <version>1.7.3</version> - <executions> - <execution> - <id>reformat-sources</id> - <configuration> - <includeStale>false</includeStale> - <style>GOOGLE</style> - <filterModified>false</filterModified> - <skip>false</skip> - <fixImports>false</fixImports> - <maxLineLength>100</maxLineLength> - </configuration> - <goals> - <goal>format</goal> - </goals> - <phase>process-sources</phase> - </execution> - </executions> - </plugin> </plugins> <pluginManagement> <plugins>