Реализовал api для UserDTO

This commit is contained in:
Никита Потапов 2024-03-17 00:16:58 +04:00
parent aab5074411
commit a7c30530f6
3 changed files with 74 additions and 1 deletions

View File

@ -2,10 +2,11 @@ package com.example.nekontakte;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class NekontakteApplication {
public static void main(String[] args) {
SpringApplication.run(NekontakteApplication.class, args);
}

View File

@ -0,0 +1,64 @@
package com.example.nekontakte;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import java.util.HashMap;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
@RestController
@RequestMapping("/user")
public class UserApiEndpoint {
private Integer lastUserId = 0;
private HashMap<Integer, UserDTO> users = new HashMap<>();
@GetMapping()
public List<UserDTO> getUsers() {
return users.values().stream().toList();
}
@GetMapping("/{id}")
public UserDTO getUser(@PathVariable Integer id) {
if (users.containsKey(id)) {
return users.get(id);
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
@PostMapping()
public UserDTO postUser(@RequestBody UserDTO entity) {
Integer currentUserId = lastUserId + 1;
entity.setId(currentUserId);
users.put(currentUserId, entity);
lastUserId++;
return entity;
}
@PutMapping("/{id}")
public UserDTO putUser(@PathVariable Integer id, @RequestBody UserDTO entity) {
if (users.containsKey(id)) {
entity.setId(id);
users.put(id, entity);
return entity;
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
@DeleteMapping("/{id}")
public UserDTO deleteUser(@PathVariable Integer id) {
if (users.containsKey(id)) {
return users.remove(id);
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
}

View File

@ -2,7 +2,10 @@ package com.example.nekontakte;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerTypePredicate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@ -13,4 +16,9 @@ public class WebConfig implements WebMvcConfigurer {
.allowedMethods("GET", "POST", "PUT", "DELETE");
}
@Override
public void configurePathMatch(@SuppressWarnings("null") PathMatchConfigurer configurer) {
configurer.addPathPrefix("api", HandlerTypePredicate.forAnnotation(RestController.class));
}
}