изменения, но новости - теги не работиают
This commit is contained in:
parent
f4337bb250
commit
e3ef535313
23
.vscode/launch.json
vendored
Normal file
23
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "java",
|
||||
"name": "Current File",
|
||||
"request": "launch",
|
||||
"mainClass": "${file}"
|
||||
},
|
||||
{
|
||||
"type": "java",
|
||||
"name": "DemoApplication",
|
||||
"request": "launch",
|
||||
"mainClass": "com.example.demo.DemoApplication",
|
||||
"projectName": "lab2",
|
||||
"args": "--populate",
|
||||
"envFile": "${workspaceFolder}/.env"
|
||||
}
|
||||
]
|
||||
}
|
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@ -1,3 +1,4 @@
|
||||
{
|
||||
"java.configuration.updateBuildConfiguration": "interactive"
|
||||
"java.configuration.updateBuildConfiguration": "interactive",
|
||||
"java.compile.nullAnalysis.mode": "automatic"
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'org.springframework.boot' version '3.2.4'
|
||||
id 'org.springframework.boot' version '3.2.5'
|
||||
id 'io.spring.dependency-management' version '1.1.4'
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,7 @@
|
||||
package com.example.demo;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -47,29 +45,35 @@ public class DemoApplication implements CommandLineRunner {
|
||||
log.info("Create default news values");
|
||||
|
||||
// tage
|
||||
final var tage1 = tageService.create(new TageEntity(null, "праздник"));
|
||||
final var tage2 = tageService.create(new TageEntity(null, "сессия"));
|
||||
final var tage3 = tageService.create(new TageEntity(null, "конкурс"));
|
||||
final var tage4 = tageService.create(new TageEntity(null, "программирование"));
|
||||
final var tage1 = tageService.create(new TageEntity("праздник"));
|
||||
final var tage2 = tageService.create(new TageEntity("сессия"));
|
||||
final var tage3 = tageService.create(new TageEntity("конкурс"));
|
||||
final var tage4 = tageService.create(new TageEntity("программирование"));
|
||||
|
||||
// new
|
||||
final NewEntity new1 = newService.create(new NewEntity(null, "Чемпионат ICPC", new Date(),
|
||||
final NewEntity new1 = newService.create(new NewEntity("Чемпионат ICPC", new Date(),
|
||||
"Студенты выступят на Чемпионате мира по программированию в Северной Евразии."));
|
||||
final NewEntity new2 = newService.create(new NewEntity(null, "Новый год 2024", new Date(2024 - 1 - 01),
|
||||
final NewEntity new2 = newService.create(new NewEntity("Новый год 2024", new Date(2024 - 1 - 01),
|
||||
"Администрация ulstu поздравляет студентов и преподавателей с Новым годом и желает крепкого здоровья и успешного Года Дракона!"));
|
||||
|
||||
final NewTageEntity newTage1 = new NewTageEntity(new1, tage1);
|
||||
final NewTageEntity newTage1 = new NewTageEntity(new1, tage3);
|
||||
new1.addTage(newTage1);
|
||||
final NewTageEntity newTage2 = new NewTageEntity(new1, tage4);
|
||||
new1.addTage(newTage2);
|
||||
final NewTageEntity newTage3 = new NewTageEntity(new2, tage1);
|
||||
new1.addTage(newTage3);
|
||||
final NewTageEntity newTage4 = new NewTageEntity(new2, tage2);
|
||||
new1.addTage(newTage4);
|
||||
|
||||
// user
|
||||
final var user1 = userService.create(new UserEntity(null, "beko", "111", "ddwwdd",
|
||||
final var user1 = userService.create(new UserEntity("beko", "111", "ddwwdd",
|
||||
"beko@mail.ru", new Date()));
|
||||
final var user2 = userService.create(new UserEntity(null, "rara", "rara", "dererere",
|
||||
final var user2 = userService.create(new UserEntity("rara", "rara", "dererere",
|
||||
"rara@mail.ru", new Date()));
|
||||
|
||||
// comment
|
||||
commentService.create(new CommentEntity(null, new Date(2024-4-04), "очень интересно", new1, user1));
|
||||
commentService.create(new CommentEntity(null, new Date(2024-4-04), "очень интересно", new2, user2));
|
||||
commentService.create(new CommentEntity(new Date(2024-4-04), "очень интересно", new1, user1));
|
||||
commentService.create(new CommentEntity(new Date(2024-4-04), "очень интересно", new2, user2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,18 +21,16 @@ public class CommentEntity extends BaseEntity {
|
||||
@Column(nullable = false, length = 200)
|
||||
private String text;
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "newsId", nullable = false)
|
||||
@JoinColumn(name = "newId", nullable = false)
|
||||
private NewEntity news;
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "userId", nullable = false)
|
||||
private UserEntity user;
|
||||
|
||||
public CommentEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public CommentEntity(Long id, Date date, String text, NewEntity news, UserEntity user) {
|
||||
super(id);
|
||||
public CommentEntity(Date date, String text, NewEntity news, UserEntity user) {
|
||||
this.date = date;
|
||||
this.text = text;
|
||||
this.news = news;
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.example.demo.core.configuration;
|
||||
|
||||
public class Constants {
|
||||
public static final String SEQUENCE_NAME = "hibernate_sequence";
|
||||
public static final String API_URL = "/api";
|
||||
|
||||
private Constants() {
|
||||
|
@ -1,15 +1,23 @@
|
||||
package com.example.demo.core.model;
|
||||
|
||||
import com.example.demo.core.configuration.Constants;
|
||||
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
import jakarta.persistence.SequenceGenerator;
|
||||
|
||||
@MappedSuperclass
|
||||
public abstract class BaseEntity {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = Constants.SEQUENCE_NAME)
|
||||
@SequenceGenerator(name = Constants.SEQUENCE_NAME, sequenceName = Constants.SEQUENCE_NAME, allocationSize = 1)
|
||||
protected Long id;
|
||||
|
||||
protected BaseEntity() {
|
||||
}
|
||||
|
||||
protected BaseEntity(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
@ -1,11 +1,8 @@
|
||||
package com.example.demo.news.api;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
@ -18,9 +15,9 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import com.example.demo.core.configuration.Constants;
|
||||
import com.example.demo.news.model.NewEntity;
|
||||
import com.example.demo.news.service.NewService;
|
||||
import com.example.demo.newtage.model.NewTageEntity;
|
||||
import com.example.demo.tages.api.TageDto;
|
||||
import com.example.demo.tages.model.TageEntity;
|
||||
import com.example.demo.tages.service.TageService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
@ -36,11 +33,13 @@ public class NewController {
|
||||
}
|
||||
|
||||
private NewDto toDto(NewEntity entity) {
|
||||
return modelMapper.map(entity, NewDto.class);
|
||||
var newdto = modelMapper.map(entity, NewDto.class);
|
||||
dto.setTages(entity.getTages().stream().map(TageEntity::getId).toList());
|
||||
return newdto;
|
||||
}
|
||||
|
||||
private NewDto toTageDto(TageEntity entity) {
|
||||
return modelMapper.map(entity, NewDto.class);
|
||||
private TageDto toTageDto(TageEntity entity) {
|
||||
return modelMapper.map(entity, TageDto.class);
|
||||
}
|
||||
|
||||
private NewEntity toEntity(NewDto dto) {
|
||||
@ -49,7 +48,9 @@ public class NewController {
|
||||
|
||||
@GetMapping
|
||||
public List<NewDto> getAll() {
|
||||
return newService.getAll().stream().map(this::toDto).toList();
|
||||
return newService.getAll().stream()
|
||||
.map(this::toDto)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@ -57,14 +58,21 @@ public class NewController {
|
||||
return toDto(newService.get(id));
|
||||
}
|
||||
|
||||
// @PostMapping
|
||||
// public NewDto create(@RequestBody @Valid NewDto dto) {
|
||||
// return toDto(newService.create(toEntity(dto)));
|
||||
// }
|
||||
|
||||
@PostMapping
|
||||
public NewDto create(@RequestBody @Valid NewDto dto) {
|
||||
return toDto(newService.create(toEntity(dto)));
|
||||
public NewDto createWithTage(@RequestBody @Valid NewDto dto) {
|
||||
return toDto(newService.create(toEntity(dto), toEntity(dto).getNewTages()));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public NewDto update(@PathVariable(name = "id") Long id, @RequestBody @Valid NewDto dto) {
|
||||
return toDto(newService.update(id, toEntity(dto)));
|
||||
public NewDto update(
|
||||
@PathVariable(name = "id") Long id,
|
||||
@RequestBody @Valid NewDto dto) {
|
||||
return toDto(newService.update(id, toEntity(dto), toEntity(dto).getNewTages()));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@ -72,35 +80,10 @@ public class NewController {
|
||||
return toDto(newService.delete(id));
|
||||
}
|
||||
|
||||
@GetMapping("/{id}/tage")
|
||||
@GetMapping("/{id}/tages")
|
||||
public List<TageDto> getNewTages(@PathVariable(name = "id") Long id) {
|
||||
return newService.getNewTages(id).stream()
|
||||
.map(this::toTageDto)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/tage")
|
||||
public List<TageDto> enableNewTages(
|
||||
@PathVariable(name = "id") Long id,
|
||||
@RequestParam(name = "tages", defaultValue = "") List<Long> tagesIds) {
|
||||
return newService.enableNewTages(id, tagesIds).stream()
|
||||
.map(this::toTageDto)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}/tage")
|
||||
public List<TageDto> disableNewTages(
|
||||
@PathVariable(name = "id") Long id,
|
||||
@RequestParam(name = "tages", defaultValue = "") List<Long> tagesIds) {
|
||||
return newService.disableNewTages(id, tagesIds).stream()
|
||||
.map(this::toTageDto)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}/tage/all")
|
||||
public List<TageDto> deleteAllNewTages(@PathVariable(name = "id") Long id) {
|
||||
return newService.deleteAllNewTages(id).stream()
|
||||
.map(this::toTageDto)
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
@ -3,10 +3,8 @@ package com.example.demo.news.api;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import com.example.demo.tage.model.TageEntity;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.PastOrPresent;
|
||||
@ -20,9 +18,9 @@ public class NewDto {
|
||||
@NotNull
|
||||
@PastOrPresent
|
||||
private Date date;
|
||||
private List<Long> tagesId;
|
||||
private List<Long> tagesId = new ArrayList<>();
|
||||
@NotBlank
|
||||
@Size(min = 1, max = 30)
|
||||
@Size(min = 1, max = 300)
|
||||
private String text;
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
|
@ -23,21 +23,19 @@ public class NewEntity extends BaseEntity {
|
||||
private String title;
|
||||
@Column(nullable = false)
|
||||
private Date date;
|
||||
@Column(nullable = false, length = 150)
|
||||
@Column(nullable = false, length = 300)
|
||||
private String text;
|
||||
@OneToMany(mappedBy = "new", cascade = CascadeType.ALL)
|
||||
@OneToMany(mappedBy = "news", cascade = CascadeType.ALL)
|
||||
@OrderBy("id ASC")
|
||||
private Set<NewTageEntity> newTages = new HashSet<>();
|
||||
@OneToMany(mappedBy = "new", cascade = CascadeType.ALL)
|
||||
@OneToMany(mappedBy = "news", cascade = CascadeType.ALL)
|
||||
@OrderBy("id ASC")
|
||||
private Set<CommentEntity> comments = new HashSet<>();
|
||||
|
||||
public NewEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public NewEntity(Long id, String title, Date date, String text) {
|
||||
super(id);
|
||||
public NewEntity(String title, Date date, String text) {
|
||||
this.title = title;
|
||||
this.date = date;
|
||||
this.text = text;
|
||||
|
@ -1,8 +1,12 @@
|
||||
package com.example.demo.news.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.example.demo.news.model.NewEntity;
|
||||
|
||||
public interface NewRepository extends CrudRepository<NewEntity, Long> {
|
||||
@Query("select n from NewEntity n where n.id = ?1")
|
||||
NewEntity findByTage(long id);
|
||||
}
|
||||
// //@Query("select n from NewsEntity n where n.tages.id = ?1")
|
||||
|
@ -1,7 +1,8 @@
|
||||
package com.example.demo.news.service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -40,21 +41,39 @@ public class NewService {
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException("Entity is null");
|
||||
}
|
||||
repository.save(entity);
|
||||
tageService.getAll().forEach(tage -> {
|
||||
final NewTageEntity userTage = new NewTageEntity(entity, tage);
|
||||
userTage.setNew(entity);
|
||||
userTage.setTage(tage);
|
||||
});
|
||||
return repository.save(entity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public NewEntity update(Long id, NewEntity entity) {
|
||||
public NewEntity create(NewEntity entity, Set<NewTageEntity> newTageList) {
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException("Entity is null");
|
||||
}
|
||||
var newTageEntities = new ArrayList<NewTageEntity>();
|
||||
for (var element : newTageList)
|
||||
{
|
||||
NewTageEntity newTage = new NewTageEntity(entity, tageService.get(element.getTage().getId()));
|
||||
newTage.setNew(entity);
|
||||
newTageEntities.add(newTage);
|
||||
entity.addTage(newTage);
|
||||
}
|
||||
return repository.save(entity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public NewEntity update(Long id, NewEntity entity, Set<NewTageEntity> newTageList) {
|
||||
final NewEntity existsEntity = get(id);
|
||||
existsEntity.setTitle(entity.getTitle());
|
||||
existsEntity.setDate(entity.getDate());
|
||||
existsEntity.setText(entity.getText());
|
||||
|
||||
existsEntity.getNewTages().clear();
|
||||
for (var element : newTageList)
|
||||
{
|
||||
NewTageEntity newTage = new NewTageEntity(entity, tageService.get(element.getTage().getId()));
|
||||
entity.addTage(newTage);
|
||||
}
|
||||
|
||||
return repository.save(existsEntity);
|
||||
}
|
||||
|
||||
@ -73,15 +92,14 @@ public class NewService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<TageEntity> deleteAllNewTages(long id) {
|
||||
final NewEntity existsNew = get(id);
|
||||
final List<NewTageEntity> tages = existsNew.getNewTages().stream()
|
||||
.filter(tage -> Objects.nonNull(tage.getTage()))
|
||||
.toList();
|
||||
tages.forEach(existsNew::deleteTage);
|
||||
repository.save(existsNew);
|
||||
return tages.stream()
|
||||
.map(NewTageEntity::getTage)
|
||||
.toList();
|
||||
public TageEntity addNewTage(long id, long tageId) {
|
||||
final NewEntity existsEntity = get(id);
|
||||
var tage = tageService.get(tageId);
|
||||
|
||||
final NewTageEntity userTage = new NewTageEntity(existsEntity, tage);
|
||||
userTage.setNew(existsEntity);
|
||||
userTage.setTage(tage);
|
||||
|
||||
return tage;
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,11 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.example.demo.core.configuration.Constants;
|
||||
import com.example.demo.news.api.NewDto;
|
||||
import com.example.demo.news.model.NewEntity;
|
||||
import com.example.demo.news.repository.NewRepository;
|
||||
import com.example.demo.tages.model.TageEntity;
|
||||
import com.example.demo.tages.repository.TageRepository;
|
||||
import com.example.demo.tages.service.TageService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
@ -23,16 +27,22 @@ import jakarta.validation.Valid;
|
||||
public class TageController {
|
||||
private final TageService tageService;
|
||||
private final ModelMapper modelMapper;
|
||||
private NewRepository newRepository;
|
||||
|
||||
public TageController(TageService tageService, ModelMapper modelMapper) {
|
||||
public TageController(TageService tageService, ModelMapper modelMapper, NewRepository newRepository) {
|
||||
this.tageService = tageService;
|
||||
this.modelMapper = modelMapper;
|
||||
this.newRepository = newRepository;
|
||||
}
|
||||
|
||||
private TageDto toDto(TageEntity entity) {
|
||||
return modelMapper.map(entity, TageDto.class);
|
||||
}
|
||||
|
||||
private NewDto toNewDto(NewEntity entity) {
|
||||
return modelMapper.map(entity, NewDto.class);
|
||||
}
|
||||
|
||||
private TageEntity toEntity(TageDto dto) {
|
||||
return modelMapper.map(dto, TageEntity.class);
|
||||
}
|
||||
@ -47,6 +57,12 @@ public class TageController {
|
||||
return toDto(tageService.get(id));
|
||||
}
|
||||
|
||||
@GetMapping("/{id}/news")
|
||||
public NewDto getTageNews(@PathVariable(name = "id") Long id) {
|
||||
var news = newRepository.findByTage(id);
|
||||
return modelMapper.map(news, NewDto.class);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public TageDto create(@RequestBody @Valid TageDto dto) {
|
||||
return toDto(tageService.create(toEntity(dto)));
|
||||
|
@ -8,7 +8,6 @@ import com.example.demo.core.model.BaseEntity;
|
||||
import com.example.demo.news.model.NewEntity;
|
||||
import com.example.demo.newtage.model.NewTageEntity;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.OneToMany;
|
||||
@ -20,16 +19,14 @@ import jakarta.persistence.Table;
|
||||
public class TageEntity extends BaseEntity{
|
||||
@Column(nullable = false, unique = true, length = 50)
|
||||
private String text;
|
||||
@OneToMany(mappedBy = "tages", cascade = CascadeType.ALL)
|
||||
@OneToMany(mappedBy = "tage")
|
||||
@OrderBy("id ASC")
|
||||
private Set<NewTageEntity> newTages = new HashSet<>();
|
||||
|
||||
public TageEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public TageEntity(Long id, String text) {
|
||||
super(id);
|
||||
public TageEntity(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
|
@ -1,11 +1,13 @@
|
||||
package com.example.demo.tages.repository;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.example.demo.news.model.NewEntity;
|
||||
import com.example.demo.tages.model.TageEntity;
|
||||
|
||||
public interface TageRepository extends CrudRepository<TageEntity, Long> {
|
||||
Optional<TageEntity> findByNameIgnoreCase(String name);
|
||||
public interface TageRepository extends JpaRepository<TageEntity, Long> {
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.news.model.NewEntity;
|
||||
import com.example.demo.newtage.model.NewTageEntity;
|
||||
import com.example.demo.tages.model.TageEntity;
|
||||
import com.example.demo.tages.repository.TageRepository;
|
||||
|
||||
@ -19,13 +21,6 @@ public class TageService {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
private void checkTage(String tage) {
|
||||
if (repository.findByNameIgnoreCase(tage).isPresent()) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Type with tage %s is already exists", tage));
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<TageEntity> getAll() {
|
||||
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
|
||||
@ -37,19 +32,24 @@ public class TageService {
|
||||
.orElseThrow(() -> new NotFoundException(TageEntity.class, id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<NewEntity> getTageNews(long id) {
|
||||
return get(id).getNewTages().stream()
|
||||
.map(NewTageEntity::getNew)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public TageEntity create(TageEntity entity) {
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException("Entity is null");
|
||||
}
|
||||
checkTage(entity.getText());
|
||||
return repository.save(entity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public TageEntity update(Long id, TageEntity entity) {
|
||||
final TageEntity existsEntity = get(id);
|
||||
checkTage(entity.getText());
|
||||
existsEntity.setText(entity.getText());
|
||||
return repository.save(existsEntity);
|
||||
}
|
||||
|
@ -34,8 +34,7 @@ public class UserEntity extends BaseEntity {
|
||||
public UserEntity() {
|
||||
}
|
||||
|
||||
public UserEntity(Long id, String login, String password, String name, String email, Date birthDate) {
|
||||
super(id);
|
||||
public UserEntity(String login, String password, String name, String email, Date birthDate) {
|
||||
this.login = login;
|
||||
this.password = password;
|
||||
this.name = name;
|
||||
|
@ -1 +1,15 @@
|
||||
|
||||
# Server
|
||||
spring.main.banner-mode=off
|
||||
|
||||
# Logger settings
|
||||
# Available levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
|
||||
logging.level.com.example.demo=DEBUG
|
||||
|
||||
# JPA Settings
|
||||
spring.datasource.url=jdbc:h2:mem:testdb
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=password
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.jpa.hibernate.ddl-auto=create
|
||||
spring.jpa.open-in-view=false
|
@ -13,7 +13,6 @@ import com.example.demo.comments.model.CommentEntity;
|
||||
import com.example.demo.comments.service.CommentService;
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.news.model.NewEntity;
|
||||
import com.example.demo.tages.model.TageEntity;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
|
||||
@SpringBootTest
|
||||
@ -31,20 +30,18 @@ class CommentServiceTest {
|
||||
@Test
|
||||
void Test() {
|
||||
//create
|
||||
var tage1 = new TageEntity(null, "пасха");
|
||||
var new1 = new NewEntity("Чемпионат ICPC", new Date(), "Студенты выступят на Чемпионате мира по программированию в Северной Евразии.");
|
||||
|
||||
var new1 = new NewEntity(null, "Чемпионат ICPC", new Date(), "Студенты выступят на Чемпионате мира по программированию в Северной Евразии.");
|
||||
var user1 = new UserEntity("beko", "111", "ddwwdd", "beko@mail.ru", new Date());
|
||||
|
||||
var user1 = new UserEntity(null, "beko", "111", "ddwwdd", "beko@mail.ru", new Date());
|
||||
|
||||
commentService.create(new CommentEntity(null, new Date(), "как красиво весной", new1, user1));
|
||||
var last = commentService.create(new CommentEntity(null, new Date(), "и ещё птицы поют", new1, user1));
|
||||
commentService.create(new CommentEntity(new Date(), "как красиво весной", new1, user1));
|
||||
var last = commentService.create(new CommentEntity(new Date(), "и ещё птицы поют", new1, user1));
|
||||
|
||||
Assertions.assertEquals(2L, commentService.getAll(0L).size());
|
||||
Assertions.assertEquals(last, commentService.get(2L));
|
||||
|
||||
//update
|
||||
final var newComment = commentService.update(1L, new CommentEntity(1L, new Date(), "это измененный коммент", new1, user1));
|
||||
final var newComment = commentService.update(1L, new CommentEntity(new Date(), "это измененный коммент", new1, user1));
|
||||
Assertions.assertEquals(2L, commentService.getAll(0L).size());
|
||||
Assertions.assertEquals(newComment, commentService.get(1L));
|
||||
|
||||
@ -53,6 +50,5 @@ class CommentServiceTest {
|
||||
Assertions.assertEquals(1L, commentService.getAll(0L).size());
|
||||
final var lastComm = commentService.get(2L);
|
||||
Assertions.assertEquals(2L, lastComm.getId());
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,27 +1,58 @@
|
||||
package com.example.demo;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.news.model.NewEntity;
|
||||
import com.example.demo.news.service.NewService;
|
||||
import com.example.demo.tages.model.TageEntity;;
|
||||
import com.example.demo.tages.model.TageEntity;
|
||||
import com.example.demo.tages.service.TageService;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
class NewsServiceTest {
|
||||
@Autowired
|
||||
private NewService newsService;
|
||||
|
||||
@Autowired
|
||||
private TageService tageService;
|
||||
|
||||
private TageEntity tage1;
|
||||
private TageEntity tage2;
|
||||
Set<TageEntity> tages = new HashSet<>();
|
||||
|
||||
@BeforeEach
|
||||
void createData() {
|
||||
removeData();
|
||||
|
||||
tage1 = tageService.create(new TageEntity("праздник"));
|
||||
tage2 = tageService.create(new TageEntity("сессия"));
|
||||
tages.add(tage1);
|
||||
tages.add(tage2);
|
||||
|
||||
// create
|
||||
newsService.create(new NewEntity("Название", new Date(), "текст новости"));
|
||||
final NewEntity last = newsService.create
|
||||
(new NewEntity("9 апреля", new Date(), "просто наступил апрель"));
|
||||
|
||||
Assertions.assertEquals(2L, newsService.getAll().size());
|
||||
Assertions.assertEquals(last, newsService.get(2L));
|
||||
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void removeData() {
|
||||
newsService.getAll().forEach(item -> newsService.delete(item.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getTest() {
|
||||
Assertions.assertThrows(NotFoundException.class, () -> newsService.get(0L));
|
||||
@ -29,22 +60,6 @@ class NewsServiceTest {
|
||||
|
||||
@Test
|
||||
void Test() {
|
||||
// create
|
||||
var tage1 = new TageEntity(null, "праздник");
|
||||
var tage2 = new TageEntity(null, "сессия");
|
||||
|
||||
newsService.create(new NewEntity(null, "Название", new Date(), "текст новости"));
|
||||
final NewEntity last = newsService.create
|
||||
(new NewEntity(null, "9 апреля", new Date(), "просто наступил апрель"));
|
||||
|
||||
Assertions.assertEquals(2L, newsService.getAll().size());
|
||||
Assertions.assertEquals(last, newsService.get(2L));
|
||||
|
||||
// update
|
||||
final var newNew = newsService.update(2L, new NewEntity(2L, "9 мая", new Date(), "уже май!!!!!!!!!"));
|
||||
Assertions.assertEquals(2L, newsService.getAll().size());
|
||||
Assertions.assertEquals(newNew, newsService.get(2L));
|
||||
|
||||
// delete
|
||||
newsService.delete(1L);
|
||||
Assertions.assertEquals(1L, newsService.getAll().size());
|
||||
|
@ -25,15 +25,15 @@ class TageServiceTest {
|
||||
@Test
|
||||
void Test() {
|
||||
//create
|
||||
tageService.create(new TageEntity(null, "праздник"));
|
||||
final var last = tageService.create(new TageEntity(null, "конкурс"));
|
||||
tageService.create(new TageEntity("праздник"));
|
||||
final var last = tageService.create(new TageEntity("конкурс"));
|
||||
|
||||
Assertions.assertEquals(2L, tageService.getAll().size());
|
||||
Assertions.assertEquals(last, tageService.get(2L));
|
||||
|
||||
//update
|
||||
final String test = "TEST";
|
||||
final var newTage = tageService.update(1L, new TageEntity(1L, test));
|
||||
final var newTage = tageService.update(1L, new TageEntity(test));
|
||||
Assertions.assertEquals(2L, tageService.getAll().size());
|
||||
Assertions.assertEquals(newTage, tageService.get(1L));
|
||||
|
||||
|
@ -28,9 +28,9 @@ class UserServiceTest {
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTest() {
|
||||
userService.create(new UserEntity(null, "beko", "111", "ddwwdd",
|
||||
userService.create(new UserEntity("beko", "111", "ddwwdd",
|
||||
"beko@mail.ru", new Date()));
|
||||
userService.create(new UserEntity(null, "rara", "rara", "dererere",
|
||||
userService.create(new UserEntity("rara", "rara", "dererere",
|
||||
"rara@mail.ru", new Date()));
|
||||
Assertions.assertEquals(2, userService.getAll().size());
|
||||
}
|
||||
@ -40,7 +40,7 @@ class UserServiceTest {
|
||||
void updateTest() {
|
||||
final String test = "TEST";
|
||||
final UserEntity newEntity = userService.update(1L,
|
||||
new UserEntity(1L, test, test, test, "test@mail.ru", new Date()));
|
||||
new UserEntity(test, test, test, "test@mail.ru", new Date()));
|
||||
Assertions.assertEquals(2L, userService.getAll().size());
|
||||
Assertions.assertEquals(newEntity, userService.get(1L));
|
||||
Assertions.assertEquals(test, newEntity.getName());
|
||||
@ -54,7 +54,7 @@ class UserServiceTest {
|
||||
final UserEntity last = userService.get(2L);
|
||||
Assertions.assertEquals(2L, last.getId());
|
||||
|
||||
final UserEntity newEntity = userService.create(new UserEntity(null, "1", "1", "1", "1@mail.ru", new Date()));
|
||||
final UserEntity newEntity = userService.create(new UserEntity("1", "1", "1", "1@mail.ru", new Date()));
|
||||
Assertions.assertEquals(2L, userService.getAll().size());
|
||||
Assertions.assertEquals(3L, newEntity.getId());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user