уф короче исправлены ошибки в сущностях "категория" и "фильм", обе сущности готовы

This commit is contained in:
ekallin 2024-03-17 23:35:30 +04:00
parent 7cbeccfd24
commit 980a89897f
15 changed files with 143 additions and 121 deletions

14
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,14 @@
{
"configurations": [
{
"type": "java",
"name": "Spring Boot-BackendApplication<backend>",
"request": "launch",
"cwd": "${workspaceFolder}",
"mainClass": "com.example.backend.BackendApplication",
"projectName": "backend",
"args": "",
"envFile": "${workspaceFolder}/.env"
}
]
}

View File

@ -19,7 +19,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'
implementation 'org.modelmapper:modelmapper:3.2.0'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.5'
// implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.5'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

View File

@ -1,63 +1,57 @@
package com.example.backend;
import java.util.ArrayList;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
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;
import com.example.backend.categories.model.CategorieEntity;
import com.example.backend.categories.service.CategorieService;
import com.example.backend.movies.model.MovieEntity;
import com.example.backend.movies.service.MovieService;
// create lab 2
@SpringBootApplication
@RestController
@RequestMapping("/api")
public class BackendApplication {
public class BackendApplication implements CommandLineRunner {
private ArrayList<TestDTO> list = new ArrayList<>();
private final Logger _logger = LoggerFactory.getLogger(BackendApplication.class);
private final CategorieService categorieService;
private final MovieService movieService;
public BackendApplication(CategorieService categorieService, MovieService movieService) {
this.categorieService = categorieService;
this.movieService = movieService;
}
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);
}
@Override
public void run(String... args) throws Exception {
@GetMapping()
public ArrayList<TestDTO> getAll(){
return list;
}
if (args.length > 0 && Objects.equals("--populate", args[0])) {
@GetMapping("/id")
public TestDTO getById(@RequestParam(name = "id") int id){
return list.get(id);
}
_logger.info("Create default categories values");
@PostMapping()
public TestDTO postTestDTO(@RequestBody TestDTO testDTO) {
list.add(testDTO);
return testDTO;
}
final var categorie1 = categorieService.create(new CategorieEntity(1, "Драма", "placeholder"));
final var categorie2 = categorieService.create(new CategorieEntity(2, "Комедия", "placeholder"));
final var categorie3 = categorieService.create(new CategorieEntity(4, "Хоррор", "placeholder"));
@PutMapping("/{id}")
public TestDTO updaTestDTO(@PathVariable(name = "id") int id, @RequestBody TestDTO entity) {
list.add(id, entity);
return entity;
}
_logger.info("Create default movies values");
@DeleteMapping("{id}")
public TestDTO delMethod(@PathVariable(name = "id") int id){
return list.remove(id);
}
movieService.create(new MovieEntity(1, categorie3, "Астрал", "", "", ""));
movieService.create(new MovieEntity(1, categorie1, "Хатико", "", "", ""));
movieService.create(new MovieEntity(1, categorie2, "Бабушка легкого поведения", "", "", ""));
movieService.create(new MovieEntity(1, categorie3, "Пиковая дама", "", "", ""));
movieService.create(new MovieEntity(1, categorie3, "Баба-Яга", "", "", ""));
}
}
}

View File

@ -15,7 +15,6 @@ import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PutMapping;
@ -59,8 +58,6 @@ public class CategorieController {
@PutMapping("/{id}")
public CategorieDTO update(@PathVariable(name = "id") Integer id, @RequestBody CategorieDTO categorieDTO) {
// TODO: process PUT request
return toDto(categorieService.update(id, toEntity(categorieDTO)));
}

View File

@ -2,41 +2,39 @@ package com.example.backend.categories.api;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.micrometer.common.lang.NonNull;
import jakarta.validation.constraints.NotBlank;
public class CategorieDTO {
@NonNull
private Integer Id;
private Integer id;
@NotBlank
private String Name;
private String name;
private String Image;
private String image;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Integer getId() {
return Id;
return id;
}
public void setId(Integer id) {
Id = id;
this.id = id;
}
public String getName() {
return Name;
return name;
}
public void setName(String name) {
Name = name;
this.name = name;
}
public String getImage() {
return Image;
return image;
}
public void setImage(String image) {
Image = image;
this.image = image;
}
}

View File

@ -6,37 +6,38 @@ import com.example.backend.core.model.BaseEntity;
public class CategorieEntity extends BaseEntity {
private String Name;
private String Image;
private String name;
private String image;
public CategorieEntity() {
}
public CategorieEntity(Integer id, String name) {
public CategorieEntity(Integer id, String name, String image) {
super(id);
Name = name;
this.name = name;
this.image = image;
}
public String getName() {
return Name;
return name;
}
public void setName(String name) {
Name = name;
this.name = name;
}
public String getImage() {
return Image;
return image;
}
public void setImage(String image) {
Image = image;
this.image = image;
}
@Override
public int hashCode() {
return Objects.hash(Id, Name, Image);
return Objects.hash(id, name, image);
}
@Override
@ -46,9 +47,9 @@ public class CategorieEntity extends BaseEntity {
if (obj == null || getClass() != obj.getClass())
return false;
final CategorieEntity other = (CategorieEntity) obj;
return Objects.equals(other.getId(), Id) &&
Objects.equals(other.getName(), Name) &&
Objects.equals(other.getImage(), Image);
return Objects.equals(other.getId(), id) &&
Objects.equals(other.getName(), name) &&
Objects.equals(other.getImage(), image);
}
}

View File

@ -1,8 +1,11 @@
package com.example.backend.categories.repository;
import org.springframework.stereotype.Repository;
import com.example.backend.categories.model.CategorieEntity;
import com.example.backend.core.repository.MapRepository;
@Repository
public class CategorieRepository extends MapRepository<CategorieEntity> {
}

View File

@ -12,34 +12,34 @@ import com.example.backend.core.errors.NotFoundException;
@Service
public class CategorieService {
private final CategorieRepository Repository;
private final CategorieRepository repository;
public CategorieService(CategorieRepository repository) {
Repository = repository;
this.repository = repository;
}
public List<CategorieEntity> getAll() {
return Repository.getAll();
return repository.getAll();
}
public CategorieEntity get(Integer id) {
return Optional.ofNullable(Repository.get(id)).orElseThrow(() -> new NotFoundException(id));
return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id));
}
public CategorieEntity create(CategorieEntity categorie) {
return Repository.create(categorie);
public CategorieEntity create(CategorieEntity entity) {
return repository.create(entity);
}
public CategorieEntity update(Integer id, CategorieEntity categorie) {
final CategorieEntity existsCategorie = get(id);
existsCategorie.setName(categorie.getName());
existsCategorie.setImage(categorie.getImage());
return Repository.update(existsCategorie);
public CategorieEntity update(Integer id, CategorieEntity entity) {
final CategorieEntity existsentity = get(id);
existsentity.setName(entity.getName());
existsentity.setImage(entity.getImage());
return repository.update(existsentity);
}
public CategorieEntity delete(Integer id) {
final CategorieEntity existsCategorie = get(id);
return Repository.delete(existsCategorie);
final CategorieEntity existsentity = get(id);
return repository.delete(existsentity);
}
}

View File

@ -1,7 +1,7 @@
package com.example.backend.core.configurations;
public class Constants {
public static final String API_URL = "/api/1.0";
public static final String API_URL = "/api";
private Constants() {
}

View File

@ -2,22 +2,22 @@ package com.example.backend.core.model;
public class BaseEntity {
public Integer Id;
public Integer id;
protected BaseEntity() {
}
protected BaseEntity(Integer id) {
Id = id;
this.id = id;
}
public Integer getId() {
return Id;
return id;
}
public void setId(Integer id) {
Id = id;
this.id = id;
}
}

View File

@ -42,7 +42,7 @@ public abstract class MapRepository<E extends BaseEntity> implements CommonRepos
@Override
public E update(E entity) {
if (checkNull(entity)) {
entities.put(lastId, entity);
entities.put(entity.getId(), entity);
return entity;
}
return null;

View File

@ -41,7 +41,7 @@ public class MovieController {
private MovieEntity toEntity(MovieDTO dto) {
final MovieEntity entity = modelMapper.map(dto, MovieEntity.class);
entity.setCategorie(categorieService.get(dto.getId()));
entity.setCategorie(categorieService.get(dto.getCategorieId()));
return entity;
}
@ -61,12 +61,12 @@ public class MovieController {
}
@PutMapping("/{id}")
public MovieDTO update(@PathVariable Integer id, @RequestBody MovieDTO dto) {
public MovieDTO update(@PathVariable(name = "id") Integer id, @RequestBody MovieDTO dto) {
return toDto(movieService.update(id, toEntity(dto)));
}
@DeleteMapping("/{id}")
public MovieDTO delete(@PathVariable Integer id) {
public MovieDTO delete(@PathVariable(name = "id") Integer id) {
return toDto(movieService.delete(id));
}

View File

@ -3,6 +3,7 @@ package com.example.backend.movies.api;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
public class MovieDTO {
@ -13,13 +14,13 @@ public class MovieDTO {
@Min(1)
private Integer categorieId;
@NotNull
@NotBlank
private String name;
@NotNull
@NotBlank
private String description;
@NotNull
@NotBlank
private String duration;
private String image;
@ -33,6 +34,14 @@ public class MovieDTO {
this.id = id;
}
public Integer getCategorieId() {
return categorieId;
}
public void setCategorieId(Integer categorieId) {
this.categorieId = categorieId;
}
public String getName() {
return name;
}

View File

@ -7,68 +7,71 @@ import com.example.backend.core.model.BaseEntity;
public class MovieEntity extends BaseEntity {
private CategorieEntity Categorie;
private CategorieEntity categorie;
private String Name;
private String Description;
private String Duration;
private String Image;
private String name;
private String description;
private String duration;
private String image;
public MovieEntity() {
super();
}
public MovieEntity(Integer id, String name, String description, String duration, String image) {
public MovieEntity(Integer id, CategorieEntity categorie, String name, String description, String duration,
String image) {
super(id);
Name = name;
Description = description;
Duration = duration;
Image = image;
this.categorie = categorie;
this.name = name;
this.description = description;
this.duration = duration;
this.image = image;
}
public CategorieEntity getCategorie() {
return Categorie;
return categorie;
}
public void setCategorie(CategorieEntity categorie) {
Categorie = categorie;
this.categorie = categorie;
}
public String getName() {
return Name;
return name;
}
public void setName(String name) {
Name = name;
this.name = name;
}
public String getDescription() {
return Description;
return description;
}
public void setDescription(String description) {
Description = description;
this.description = description;
}
public String getDuration() {
return Duration;
return duration;
}
public void setDuration(String duration) {
Duration = duration;
this.duration = duration;
}
public String getImage() {
return Image;
return image;
}
public void setImage(String image) {
Image = image;
this.image = image;
}
@Override
public int hashCode() {
return Objects.hash(Id, Categorie, Name, Description, Duration, Image);
return Objects.hash(id, categorie, name, description, duration, image);
}
@Override
@ -78,12 +81,12 @@ public class MovieEntity extends BaseEntity {
if (obj == null || getClass() != obj.getClass())
return false;
final MovieEntity other = (MovieEntity) obj;
return Objects.equals(other.getId(), Id)
&& Objects.equals(other.getCategorie(), Categorie)
&& Objects.equals(other.getName(), Name)
&& Objects.equals(other.getDescription(), Description)
&& Objects.equals(other.getDuration(), Duration)
&& Objects.equals(other.getImage(), Image);
return Objects.equals(other.getId(), id)
&& Objects.equals(other.getCategorie(), categorie)
&& Objects.equals(other.getName(), name)
&& Objects.equals(other.getDescription(), description)
&& Objects.equals(other.getDuration(), duration)
&& Objects.equals(other.getImage(), image);
}
}

View File

@ -1,8 +1,11 @@
package com.example.backend.movies.repository;
import org.springframework.stereotype.Repository;
import com.example.backend.core.repository.MapRepository;
import com.example.backend.movies.model.MovieEntity;
@Repository
public class MovieRepository extends MapRepository<MovieEntity> {
}