Add user and project rate editing across backend and frontend
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
package com.example.app.user;
|
||||
|
||||
import com.example.app.user.UserDtos.UpdateUserRequest;
|
||||
import com.example.app.user.UserDtos.UserResponse;
|
||||
import jakarta.validation.Valid;
|
||||
import java.util.List;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@@ -20,4 +25,9 @@ public class UserController {
|
||||
public List<UserResponse> list() {
|
||||
return userService.findAll();
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public UserResponse update(@PathVariable Long id, @Valid @RequestBody UpdateUserRequest req) {
|
||||
return userService.update(id, req);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
package com.example.app.user;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.PositiveOrZero;
|
||||
|
||||
public class UserDtos {
|
||||
|
||||
public record UpdateUserRequest(
|
||||
@NotBlank String name, @PositiveOrZero int defaultHourlyRateCents) {}
|
||||
|
||||
public record UserResponse(Long id, String name, int defaultHourlyRateCents) {
|
||||
|
||||
public static UserResponse from(User user) {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.example.app.user;
|
||||
|
||||
import com.example.app.user.UserDtos.UpdateUserRequest;
|
||||
import com.example.app.user.UserDtos.UserResponse;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import java.util.List;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -18,4 +20,16 @@ public class UserService {
|
||||
public List<UserResponse> findAll() {
|
||||
return userRepository.findAll().stream().map(UserResponse::from).toList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UserResponse update(Long id, UpdateUserRequest req) {
|
||||
User user =
|
||||
userRepository
|
||||
.findById(id)
|
||||
.orElseThrow(() -> new EntityNotFoundException("User not found: " + id));
|
||||
user.setName(req.name());
|
||||
user.setDefaultHourlyRateCents(req.defaultHourlyRateCents());
|
||||
User saved = userRepository.save(user);
|
||||
return UserResponse.from(saved);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user