diff --git a/backend/build.gradle b/backend/build.gradle index 1c63230..734ff8e 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -16,7 +16,9 @@ repositories { } dependencies { - implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-actuator' + implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0' testImplementation 'org.springframework.boot:spring-boot-starter-test' } diff --git a/backend/src/main/java/com/example/backend/BackendApplication.java b/backend/src/main/java/com/example/backend/BackendApplication.java index 450efbc..f6ee5ee 100644 --- a/backend/src/main/java/com/example/backend/BackendApplication.java +++ b/backend/src/main/java/com/example/backend/BackendApplication.java @@ -1,13 +1,63 @@ package com.example.backend; +import java.util.ArrayList; + import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +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; + + @SpringBootApplication +@RestController +@RequestMapping("/api") public class BackendApplication { + private ArrayList list = new ArrayList<>(); + public static void main(String[] args) { SpringApplication.run(BackendApplication.class, args); } + @GetMapping("/hello") + public String get(@RequestParam(name = "name", defaultValue = "World!") String name){ + return String.format("Hello, %s", name); + } + + @GetMapping() + public ArrayList getAll(){ + return list; + } + + @GetMapping("/id") + public TestDTO getById(@RequestParam(name = "id") int id){ + return list.get(id); + } + + @PostMapping() + public TestDTO postTestDTO(@RequestBody TestDTO testDTO) { + list.add(testDTO); + return testDTO; + } + + @PutMapping("/{id}") + public TestDTO updaTestDTO(@PathVariable(name = "id") int id, @RequestBody TestDTO entity) { + list.add(id, entity); + return entity; + } + + + @DeleteMapping("{id}") + public TestDTO delMethod(@PathVariable(name = "id") int id){ + return list.remove(id); + } + } diff --git a/backend/src/main/java/com/example/backend/TestDTO.java b/backend/src/main/java/com/example/backend/TestDTO.java new file mode 100644 index 0000000..386d995 --- /dev/null +++ b/backend/src/main/java/com/example/backend/TestDTO.java @@ -0,0 +1,55 @@ +package com.example.backend; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; + +public class TestDTO { + + private Integer id; + private String name; + private String img; + + public TestDTO() { + + } + + @JsonCreator + public TestDTO( + @JsonProperty(value = "id") Integer id, + @JsonProperty(value = "name") String name, + @JsonProperty(value = "img") String img + ) + { + this.id = id; + this.name = name; + this.img = img; + } + + // GET + + public Integer getId() { + return id; + } + + public String getName() { + return name; + } + + public String getImg(){ + return img; + } + + // SET + + public void setId(Integer Id) { + id = Id; + } + + public void setName(String Name) { + name = Name; + } + + public void setImg(String Img){ + img = Img; + } +} diff --git a/backend/src/main/java/com/example/backend/WebConfig.java b/backend/src/main/java/com/example/backend/WebConfig.java new file mode 100644 index 0000000..f39f0d6 --- /dev/null +++ b/backend/src/main/java/com/example/backend/WebConfig.java @@ -0,0 +1,16 @@ +package com.example.backend; + +import org.springframework.context.annotation.Configuration; +import org.springframework.lang.NonNull; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +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"); + + } +}