-
Notifications
You must be signed in to change notification settings - Fork 2
UserController, UserService & UserDTO #133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
733e20e
e0dacbc
f74cb70
1644951
fb41acf
b10c3cb
f4725ec
ea43114
0eb37e2
baab678
591c8eb
934a2a2
0ccf7f1
4211014
8dc0764
a97112a
41cd3b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package org.example.vet1177.controller; | ||
|
|
||
| import jakarta.validation.Valid; | ||
| import org.example.vet1177.dto.request.user.UserRequest; | ||
| import org.example.vet1177.dto.response.user.UserResponse; | ||
| import org.example.vet1177.services.UserService; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import org.example.vet1177.dto.request.user.UserUpdateRequest; | ||
|
|
||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/users") | ||
| public class UserController { | ||
|
|
||
| private final UserService userService; | ||
|
|
||
| public UserController(UserService userService) { | ||
| this.userService = userService; | ||
| } | ||
|
|
||
| //GET /users- Hämta alla användare | ||
| @GetMapping | ||
| public ResponseEntity<List<UserResponse>> getAllUsers() { | ||
| List<UserResponse> users = userService.getAllUsers(); | ||
| return ResponseEntity.ok(users); | ||
| } | ||
|
|
||
| // GET /users/{id}- Hämta 1 användare | ||
| @GetMapping("/{id}") | ||
| public ResponseEntity<UserResponse> getUserById(@PathVariable UUID id) { | ||
| UserResponse user = userService.getById(id); | ||
| return ResponseEntity.ok(user); | ||
| } | ||
|
|
||
| //POST /users - skapa ny användare | ||
| @PostMapping | ||
| public ResponseEntity<UserResponse> createUser(@Valid @RequestBody UserRequest request) { | ||
| UserResponse user = userService.createUser(request); | ||
| return ResponseEntity.status(HttpStatus.CREATED).body(user); | ||
| } | ||
|
|
||
| //PUT /users/{id} - uppdatera användare | ||
| @PutMapping("/{id}") | ||
| public ResponseEntity<UserResponse> updateUser(@PathVariable UUID id, | ||
| @Valid @RequestBody UserUpdateRequest request) { | ||
| UserResponse user = userService.updateUser(id, request); | ||
| return ResponseEntity.ok(user); | ||
| } | ||
|
Comment on lines
+40
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Verify GlobalExceptionHandler doesn't handle IllegalArgumentException
rg -n "IllegalArgumentException" --type=java src/main/java/org/example/vet1177/exception/Repository: ithsjava25/project-backend-org-random-coders Length of output: 70 🏁 Script executed: fd -t f "GlobalExceptionHandler" src/main/java/Repository: ithsjava25/project-backend-org-random-coders Length of output: 159 🏁 Script executed: cat -n src/main/java/org/example/vet1177/exception/GlobalExceptionHandler.javaRepository: ithsjava25/project-backend-org-random-coders Length of output: 3164 🏁 Script executed: rg -n "IllegalArgumentException" --type=java src/main/java/ -A 3 -B 1Repository: ithsjava25/project-backend-org-random-coders Length of output: 6319 🏁 Script executed: rg -n "throw new IllegalArgumentException" --type=java src/main/java/Repository: ithsjava25/project-backend-org-random-coders Length of output: 1996
The GlobalExceptionHandler does not have an Consider either:
🤖 Prompt for AI Agents |
||
|
|
||
| //DELETE /users/{id} - Tar bort användare | ||
| @DeleteMapping("/{id}") | ||
| public ResponseEntity<Void> deleteUser(@PathVariable UUID id) { | ||
| userService.deleteUser(id); | ||
| return ResponseEntity.noContent().build(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package org.example.vet1177.dto.request.user; | ||
|
|
||
| import jakarta.validation.constraints.Email; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import jakarta.validation.constraints.Size; | ||
| import org.example.vet1177.entities.Role; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| public class UserRequest { | ||
|
|
||
| @NotBlank | ||
| @Size(min = 2, max = 100) | ||
| private String name; | ||
|
|
||
| @NotBlank | ||
| private String email; | ||
|
|
||
| @NotBlank | ||
| @Size(min = 8, max = 100) | ||
| private String password; | ||
|
|
||
| @NotNull | ||
| private Role role; | ||
|
|
||
| private UUID clinicId; | ||
|
|
||
| public UserRequest(){ | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String getEmail() { | ||
| return email; | ||
| } | ||
|
|
||
| public void setEmail(String email) { | ||
| this.email = email; | ||
| } | ||
|
|
||
| public String getPassword() { | ||
| return password; | ||
| } | ||
|
|
||
| public void setPassword(String password) { | ||
| this.password = password; | ||
| } | ||
|
|
||
| public Role getRole() { | ||
| return role; | ||
| } | ||
|
|
||
| public void setRole(Role role) { | ||
| this.role = role; | ||
| } | ||
|
|
||
| public UUID getClinicId() { | ||
| return clinicId; | ||
| } | ||
|
|
||
| public void setClinicId(UUID clinicId) { | ||
| this.clinicId = clinicId; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package org.example.vet1177.dto.request.user; | ||
|
|
||
| import jakarta.validation.constraints.Email; | ||
| import jakarta.validation.constraints.Size; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| public class UserUpdateRequest { | ||
|
|
||
| @Size(min = 2, max = 100) | ||
| private String name; | ||
|
|
||
| private String email; | ||
|
|
||
| private UUID clinicId; | ||
|
|
||
| public UUID getClinicId() { | ||
| return clinicId; | ||
| } | ||
|
|
||
| public void setClinicId(UUID clinicId) { | ||
| this.clinicId = clinicId; | ||
| } | ||
|
|
||
| public String getEmail() { | ||
| return email; | ||
| } | ||
|
|
||
| public void setEmail(String email) { | ||
| this.email = email; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package org.example.vet1177.dto.response.user; | ||
|
|
||
| import org.example.vet1177.entities.Role; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.UUID; | ||
|
|
||
| public class UserResponse { | ||
|
|
||
| private UUID id; | ||
| private String name; | ||
| private String email; | ||
| private Role role; | ||
| private UUID clinicId; | ||
| private Instant createdAt; | ||
| private Instant updatedAt; | ||
|
|
||
| public UserResponse() { | ||
| } | ||
|
|
||
| public UserResponse(UUID id, String name, String email, Role role, UUID clinicId, Instant createdAt, Instant updatedAt) { | ||
| this.id =id; | ||
| this.name =name; | ||
| this.email = email; | ||
| this.role = role; | ||
| this.clinicId = clinicId; | ||
| this.createdAt = createdAt; | ||
| this.updatedAt =updatedAt; | ||
| } | ||
|
|
||
| public UUID getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(UUID id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String getEmail() { | ||
| return email; | ||
| } | ||
|
|
||
| public void setEmail(String email) { | ||
| this.email = email; | ||
| } | ||
|
|
||
| public Role getRole() { | ||
| return role; | ||
| } | ||
|
|
||
| public void setRole(Role role) { | ||
| this.role = role; | ||
| } | ||
|
|
||
| public UUID getClinicId() { | ||
| return clinicId; | ||
| } | ||
|
|
||
| public void setClinicId(UUID clinicId) { | ||
| this.clinicId = clinicId; | ||
| } | ||
|
|
||
| public Instant getCreatedAt() { | ||
| return createdAt; | ||
| } | ||
|
|
||
| public void setCreatedAt(Instant createdAt) { | ||
| this.createdAt = createdAt; | ||
| } | ||
|
|
||
| public Instant getUpdatedAt() { | ||
| return updatedAt; | ||
| } | ||
|
|
||
| public void setUpdatedAt(Instant updatedAt) { | ||
| this.updatedAt = updatedAt; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.