mirror of
https://github.com/Lemonochrme/service-architecture.git
synced 2025-06-08 05:30:50 +02:00
Implement user management endpoints using hashmap storage for now (we will have to implement database later): create, retrieve, update, and delete users
This commit is contained in:
parent
aab5cf3f3d
commit
a38b30b3c2
1 changed files with 83 additions and 5 deletions
|
@ -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<Long, User> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue