Skip to content
Open

JPA #602

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions 351004/Brazhalovich/Lab_1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.2</version>
<relativePath/>
</parent>

<groupId>org.example</groupId>
<artifactId>newsapi</artifactId>
<version>1.0-SNAPSHOT</version>
<name>newsapi</name>
<description>REST API with JPA and Liquibase</description>

<properties>
<java.version>21</java.version>
<org.mapstruct.version>1.5.5.Final</org.mapstruct.version>
<lombok.version>1.18.30</lombok.version>
</properties>

<dependencies>
<!-- Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- JPA / Hibernate (New) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<!-- PostgreSQL Driver (New) -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>

<!-- Liquibase Migration (New) -->
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>

<!-- Validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>

<!-- MapStruct -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>

<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>

<!-- TestContainers (New) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.example.newsapi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class NewsApiApplication {
public static void main(String[] args) {
SpringApplication.run(NewsApiApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.example.newsapi.controller;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.example.newsapi.dto.request.CommentRequestTo;
import org.example.newsapi.dto.response.CommentResponseTo;
import org.example.newsapi.service.CommentService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1.0/comments")
@RequiredArgsConstructor
public class CommentController {

private final CommentService commentService;

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public CommentResponseTo create(@RequestBody @Valid CommentRequestTo request) {
return commentService.create(request);
}

@GetMapping
public List<CommentResponseTo> getAll(@PageableDefault(size = 50) Pageable pageable) {
return commentService.findAll(pageable).getContent();
}

@GetMapping("/{id}")
public CommentResponseTo getById(@PathVariable Long id) {
return commentService.findById(id);
}

@PutMapping("/{id}")
public CommentResponseTo update(@PathVariable Long id, @RequestBody @Valid CommentRequestTo request) {
return commentService.update(id, request);
}

@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@PathVariable Long id) {
commentService.delete(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.example.newsapi.controller;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.example.newsapi.dto.request.MarkerRequestTo;
import org.example.newsapi.dto.response.MarkerResponseTo;
import org.example.newsapi.service.MarkerService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1.0/markers")
@RequiredArgsConstructor
public class MarkerController {

private final MarkerService markerService;

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public MarkerResponseTo create(@RequestBody @Valid MarkerRequestTo request) { // ПРОВЕРЬ @Valid
return markerService.create(request);
}

@GetMapping
public List<MarkerResponseTo> getAll(@PageableDefault(size = 50) Pageable pageable) {
return markerService.findAll(pageable).getContent();
}

@GetMapping("/{id}")
public MarkerResponseTo getById(@PathVariable Long id) {
return markerService.findById(id);
}

@PutMapping("/{id}")
public MarkerResponseTo update(@PathVariable Long id, @RequestBody @Valid MarkerRequestTo request) {
return markerService.update(id, request);
}

@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@PathVariable Long id) {
markerService.delete(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.example.newsapi.controller;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.example.newsapi.dto.request.NewsRequestTo;
import org.example.newsapi.dto.response.NewsResponseTo;
import org.example.newsapi.service.NewsService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1.0/news")
@RequiredArgsConstructor
public class NewsController {

private final NewsService newsService;

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public NewsResponseTo create(@RequestBody @Valid NewsRequestTo request) {
System.out.println(">>> CREATE NEWS REQUEST: " + request);
System.out.println(">>> markerNames: " + request.getMarkerNames());
return newsService.create(request);
}

@GetMapping
public List<NewsResponseTo> getAll(@PageableDefault(size = 50) Pageable pageable) {
return newsService.findAll(pageable).getContent();
}

@GetMapping("/{id}")
public NewsResponseTo getById(@PathVariable Long id) {
return newsService.findById(id);
}

@PutMapping("/{id}")
public NewsResponseTo update(@PathVariable Long id, @RequestBody @Valid NewsRequestTo request) {
return newsService.update(id, request);
}

@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@PathVariable Long id) {
newsService.delete(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.example.newsapi.controller;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.example.newsapi.dto.request.UserRequestTo;
import org.example.newsapi.dto.response.UserResponseTo;
import org.example.newsapi.service.UserService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1.0/users")
@RequiredArgsConstructor
public class UserController {

private final UserService userService;

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public UserResponseTo create(@RequestBody @Valid UserRequestTo request) {
return userService.create(request);
}

@GetMapping
public List<UserResponseTo> getAll(@PageableDefault(size = 50) Pageable pageable) {
// Возвращаем только контент списка, чтобы тестер не путался в мета-данных Page
return userService.findAll(pageable).getContent();
}

@GetMapping("/{id}")
public UserResponseTo getById(@PathVariable Long id) {
return userService.findById(id);
}

@PutMapping("/{id}")
public UserResponseTo update(@PathVariable Long id, @RequestBody @Valid UserRequestTo request) {
return userService.update(id, request);
}

@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@PathVariable Long id) {
userService.delete(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.example.newsapi.dto.request;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Data;

@Data
public class CommentRequestTo {

//@JsonProperty("news")
@NotNull
private Long newsId;

@Size(min = 2, max = 2048)
private String content;

public void setNews(Long news) {
this.newsId = news;
}
}
Loading