From a38b30b3c213817f90272fac5bc489d0a3f7b0c4 Mon Sep 17 00:00:00 2001 From: Lemonochrme Date: Sat, 14 Dec 2024 17:12:22 +0100 Subject: [PATCH] Implement user management endpoints using hashmap storage for now (we will have to implement database later): create, retrieve, update, and delete users --- .../helpapp/rest/UserServiceApplication.java | 88 +++++++++++++++++-- 1 file changed, 83 insertions(+), 5 deletions(-) diff --git a/helpapp/user-service/src/main/java/insa/application/helpapp/rest/UserServiceApplication.java b/helpapp/user-service/src/main/java/insa/application/helpapp/rest/UserServiceApplication.java index 981d27c..a2d7b66 100644 --- a/helpapp/user-service/src/main/java/insa/application/helpapp/rest/UserServiceApplication.java +++ b/helpapp/user-service/src/main/java/insa/application/helpapp/rest/UserServiceApplication.java @@ -2,19 +2,97 @@ package insa.application.helpapp.rest; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; + +import java.util.*; +import java.util.concurrent.atomic.AtomicLong; @SpringBootApplication @RestController +@RequestMapping("/users") public class UserServiceApplication { + private final Map userDatabase = new HashMap<>(); + private final AtomicLong idGenerator = new AtomicLong(1); + public static void main(String[] args) { SpringApplication.run(UserServiceApplication.class, args); } - @GetMapping("/hello") - public String hello() { - return "Hello from User Service!"; + // Create a new user + @PostMapping + public User createUser(@RequestBody User user) { + long id = idGenerator.getAndIncrement(); + user.setId(id); + userDatabase.put(id, user); + return user; + } + + // Get a user by ID + @GetMapping("/{id}") + public User getUser(@PathVariable Long id) { + return Optional.ofNullable(userDatabase.get(id)) + .orElseThrow(() -> new RuntimeException("User not found")); + } + + // Update a user by ID + @PutMapping("/{id}") + public User updateUser(@PathVariable Long id, @RequestBody User updatedUser) { + if (!userDatabase.containsKey(id)) { + throw new RuntimeException("User not found"); + } + updatedUser.setId(id); + userDatabase.put(id, updatedUser); + return updatedUser; + } + + // Delete a user by ID + @DeleteMapping("/{id}") + public String deleteUser(@PathVariable Long id) { + if (userDatabase.remove(id) == null) { + throw new RuntimeException("User not found"); + } + return "User deleted successfully"; + } + + // User entity + static class User { + private Long id; + private String name; + private String email; + private String role; + + // Getters and setters + public Long getId() { + return id; + } + + public void setId(Long 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 String getRole() { + return role; + } + + public void setRole(String role) { + this.role = role; + } } }