Skip to content
Snippets Groups Projects
Commit e1aba639 authored by Marek Skácelík's avatar Marek Skácelík
Browse files

Added UPDATE/DELETE endpoints for user

parent a6a49858
No related branches found
No related tags found
2 merge requests!25Milestone 1,!9openAPI documentation for user WIP
Pipeline #
......@@ -3,6 +3,7 @@ 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 jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
......@@ -15,16 +16,12 @@ import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/user")
public class UserController {
private final UserFacade userFacade;
private final UserService service;
private final UserMapper mapper;
@Autowired
public UserController(UserService service, UserMapper mapper) {
this.service = service;
this.mapper = mapper;
}
@Autowired
public UserController(UserFacade userFacade) {
this.userFacade = userFacade;
}
@Operation(
summary = "Find user by ID",
......@@ -43,9 +40,9 @@ public class UserController {
@ApiResponse(responseCode = "404", description = "User not found", content = @Content)
})
@GetMapping("/{id}")
public UserDto find(
public UserDto findById(
@Parameter(description = "ID of user to be searched") @PathVariable String id) {
return mapper.toDto(service.find(id));
return userFacade.findById(id);
}
@Operation(
......@@ -70,8 +67,8 @@ public class UserController {
})
@PostMapping
public UserDto create(
@Parameter(description = "User to be created") @RequestBody UserCreateDto dto) {
return mapper.toDto(service.create(mapper.fromCreateDto(dto)));
@Parameter(description = "User to be created") @RequestBody UserCreateDto userCreateDto) {
return userFacade.create(userCreateDto);
}
@Operation(
......@@ -92,29 +89,32 @@ public class UserController {
@GetMapping
public Result<UserDto> findAll(
@Parameter(description = "Page number of results to retrieve") @RequestParam int page) {
return mapper.toResult(service.findAllPageableInt(page));
return userFacade.findAll(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)));
}
@PutMapping("/{id}")
@Operation(summary = "Update a user by ID")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "User updated successfully"),
@ApiResponse(responseCode = "400", description = "Invalid input"),
@ApiResponse(responseCode = "404", description = "User not found"),
@ApiResponse(responseCode = "500", description = "Internal server error")
})
public UserDto update(
@Parameter(description = "ID of the user to be updated") @PathVariable String id,
@RequestBody @Valid UserCreateDto userCreateDto) {
return userFacade.update(userCreateDto, id);
}
@DeleteMapping("/{id}")
@Operation(summary = "Delete a user by ID")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "User deleted successfully"),
@ApiResponse(responseCode = "404", description = "User not found"),
@ApiResponse(responseCode = "500", description = "Internal server error")
})
public UserDto delete(
@Parameter(description = "ID of the user to be deleted") @PathVariable String id) {
return userFacade.delete(id);
}
}
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.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@Component
@Transactional
public class UserFacade {
private final UserService userService;
private final UserMapper userMapper;
@Autowired
public UserFacade(UserService userService, UserMapper userMapper) {
this.userService = userService;
this.userMapper = userMapper;
}
@Transactional(readOnly = true)
public UserDto findById(String id) {
return userMapper.toDto(userService.findById(id));
}
@Transactional(readOnly = true)
public Result<UserDto> findAll(int page) {
return userMapper.toResult(userService.findAllPageableInt(page));
}
@Transactional(readOnly = true)
public UserDto create(UserCreateDto userCreateDto) {
return userMapper.toDto(userService.create(userMapper.fromCreateDto(userCreateDto)));
}
@Transactional
public UserDto delete(String id) {
return userMapper.toDto(userService.deleteById(id));
}
@Transactional
public UserDto update(UserCreateDto userUpdatedDto, String id) {
return userMapper.toDto(userService.update(userMapper.fromCreateDto(userUpdatedDto), id));
}
}
......@@ -7,13 +7,10 @@ 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);
}
package cz.muni.fi.pa165.core.user;
import cz.muni.fi.pa165.core.device.Device;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface UserRepository extends JpaRepository<User, String> {}
public interface UserRepository extends JpaRepository<User, String> {
@Modifying
@Query("UPDATE User u set u.username= :#{#user.username}, u.password= :#{#user.password}, u.lastName= :#{#user.lastName}, u.firstName= :#{#user.firstName}, u.email= :#{#user.email} where u.id = :#{#id}")
int update(@Param("user") User user, @Param("id") String id);
}
......@@ -7,8 +7,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class UserService extends DomainService<User> {
......@@ -20,7 +18,7 @@ public class UserService extends DomainService<User> {
}
@Transactional(readOnly = true)
public User find(String id) {
public User findById(String id) {
return repository
.findById(id)
.orElseThrow(() -> new EntityNotFoundException("User '" + id + "' not found."));
......@@ -30,4 +28,23 @@ public class UserService extends DomainService<User> {
public void deleteAll() {
repository.deleteAll();
}
@Transactional(readOnly = true)
public User deleteById(String id) {
User result = findById(id);
if (result == null)
throw new EntityNotFoundException("User '" + id + "' not found.");
repository.deleteById(id);
return result;
}
@Transactional(readOnly = true)
public User update(User user, String id) {
int result = repository.update(user, id);
if (result != 1) {
throw new EntityNotFoundException("User '" + id + "' not found.");
}
return findById(id);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment