Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
a7c30530f6 | |||
aab5074411 | |||
2e45bfca54 |
@ -2,10 +2,11 @@ package com.example.nekontakte;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@RestController
|
||||||
public class NekontakteApplication {
|
public class NekontakteApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(NekontakteApplication.class, args);
|
SpringApplication.run(NekontakteApplication.class, args);
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
31
nekontakte/src/main/java/com/example/nekontakte/UserDTO.java
Normal file
31
nekontakte/src/main/java/com/example/nekontakte/UserDTO.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package com.example.nekontakte;
|
||||||
|
|
||||||
|
public class UserDTO {
|
||||||
|
private Integer id;
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Integer id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
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
|
||||||
|
public class WebConfig implements WebMvcConfigurer {
|
||||||
|
@Override
|
||||||
|
public void addCorsMappings(@NonNull CorsRegistry registry) {
|
||||||
|
registry.addMapping("/**")
|
||||||
|
.allowedMethods("GET", "POST", "PUT", "DELETE");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void configurePathMatch(@SuppressWarnings("null") PathMatchConfigurer configurer) {
|
||||||
|
configurer.addPathPrefix("api", HandlerTypePredicate.forAnnotation(RestController.class));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user