Сделанные сущности. Необходимо починить тесты и DemoApplication

This commit is contained in:
DyCTaTOR 2024-04-14 21:41:28 +04:00
parent abd50a63ed
commit c4eb1070e1
20 changed files with 195 additions and 93 deletions

BIN
data.mv.db Normal file

Binary file not shown.

View File

@ -5,11 +5,11 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotNull;
public class DepartmentDto {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Long id;
@NotNull
private String name;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Long getId() {
return id;
}

View File

@ -17,7 +17,7 @@ public class DepartmentEntity extends BaseEntity {
public DepartmentEntity() {
}
public DepartmentEntity(Long id, String name) {
public DepartmentEntity(String name) {
this.name = name;
}

View File

@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.core.configuration.Constants;
@ -44,8 +45,8 @@ public class DirectionsController {
}
@GetMapping
public List<DirectionsDto> getAll() {
return directionsService.getAll().stream().map(this::toDto).toList();
public List<DirectionsDto> getAll(@RequestParam(name = "departmentId", defaultValue = "0") Long departmentId) {
return directionsService.getAll(departmentId).stream().map(this::toDto).toList();
}
@GetMapping("/{id}")

View File

@ -7,6 +7,7 @@ import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
public class DirectionsDto {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Long id;
@NotBlank
private String code;
@ -17,7 +18,6 @@ public class DirectionsDto {
private Long departmentId;
private String things;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Long getId() {
return id;
}

View File

@ -27,7 +27,7 @@ public class DirectionsEntity extends BaseEntity {
public DirectionsEntity() {
}
public DirectionsEntity(Long id, String code, String name, DepartmentEntity department, String things) {
public DirectionsEntity(String code, String name, DepartmentEntity department, String things) {
this.code = code;
this.name = name;
this.department = department;

View File

@ -1,31 +1,14 @@
package com.example.demo.directions.repository;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import com.example.demo.directions.model.DirectionsEntity;
public interface DirectionsRepository extends CrudRepository<DirectionsEntity, Long> {
Optional<OrderEntity> findOneByUserIdAndId(long userId, long id);
Optional<DirectionsEntity> findByNameIgnoreCase(String name);
List<OrderEntity> findByUserId(long userId);
List<OrderEntity> findByUserIdAndTypeId(long userId, long typeId);
// select
// tpe.name,
// coalesce(sum(order.price), 0),
// coalesce(sum(order.count), 0)
// from types as tpe
// left join orders as order on tpe.id = order.type_id and order.user_id = ?
// group by tpe.name order by tpe.id
@Query("select "
+ "t as type, "
+ "coalesce(sum(o.price), 0) as totalPrice, "
+ "coalesce(sum(o.count), 0) as totalCount "
+ "from TypeEntity t left join OrderEntity o on o.type = t and o.user.id = ?1 "
+ "group by t order by t.id")
List<OrderGrouped> getOrdersTotalByType(long userId);
List<DirectionsEntity> findByDepartmentId(long departmentId);
}

View File

@ -1,9 +1,10 @@
package com.example.demo.directions.service;
import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.directions.model.DirectionsEntity;
@ -13,34 +14,56 @@ import com.example.demo.directions.repository.DirectionsRepository;
public class DirectionsService {
private final DirectionsRepository repository;
private void checkName(String name) {
if (repository.findByNameIgnoreCase(name).isPresent()) {
throw new IllegalArgumentException(
String.format("Type with name %s is already exists", name));
}
}
public DirectionsService(DirectionsRepository repository) {
this.repository = repository;
}
public List<DirectionsEntity> getAll() {
return repository.getAll();
@Transactional(readOnly = true)
public List<DirectionsEntity> getAll(Long departmentId) {
if (departmentId <= 0L) {
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
} else {
return repository.findByDepartmentId(departmentId);
}
}
@Transactional(readOnly = true)
public DirectionsEntity get(Long id) {
return Optional.ofNullable(repository.get(id))
.orElseThrow(() -> new NotFoundException(id));
return repository.findById(id)
.orElseThrow(() -> new NotFoundException(DirectionsEntity.class, id));
}
@Transactional
public DirectionsEntity create(DirectionsEntity entity) {
return repository.create(entity);
if (entity == null) {
throw new IllegalArgumentException("Entity is null");
}
checkName(entity.getName());
return repository.save(entity);
}
@Transactional
public DirectionsEntity update(Long id, DirectionsEntity entity) {
final DirectionsEntity existsEntity = get(id);
checkName(entity.getName());
existsEntity.setCode(entity.getCode());
existsEntity.setName(entity.getName());
existsEntity.setDepartment(entity.getDepartment());
existsEntity.setThings(entity.getThings());
return repository.update(existsEntity);
return repository.save(existsEntity);
}
@Transactional
public DirectionsEntity delete(Long id) {
final DirectionsEntity existsEntity = get(id);
return repository.delete(existsEntity);
repository.delete(existsEntity);
return existsEntity;
}
}

View File

@ -45,8 +45,8 @@ public class EntrysDataController {
}
@GetMapping
public List<EntrysDataDto> getAll() {
return entrysDataService.getAll().stream().map(this::toDto).toList();
public List<EntrysDataDto> getAll(@RequestParam(name = "departmentId", defaultValue = "0") Long departmentId) {
return entrysDataService.getAll(departmentId).stream().map(this::toDto).toList();
}
@GetMapping("/{id}")
@ -69,8 +69,8 @@ public class EntrysDataController {
return toDto(entrysDataService.delete(id));
}
@PutMapping("/{id2}")
public EntrysDataDto updatePassword(@PathVariable(name = "id") Long id,
@PutMapping("/{id}")
public EntrysDataDto updatePassword(@PathVariable(name = "password") Long id,
@RequestParam(name = "newPassword") String newPas,
@RequestParam(name = "oldPassword") String oldPas) {
return toDto(entrysDataService.updatePassword(id, newPas, oldPas));

View File

@ -7,6 +7,7 @@ import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
public class EntrysDataDto {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Long id;
@NotNull
@Min(1)
@ -18,7 +19,6 @@ public class EntrysDataDto {
@NotBlank
private String password;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Long getId() {
return id;
}

View File

@ -5,18 +5,29 @@ import java.util.Objects;
import com.example.demo.core.model.BaseEntity;
import com.example.demo.department.model.DepartmentEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
@Entity
@Table(name = "entrysData")
public class EntrysDataEntity extends BaseEntity {
@Column(nullable = false, unique = true, length = 30)
private String login;
@Column(nullable = false, unique = false, length = 25)
private String password;
@Column(nullable = false, unique = false, length = 10)
private String role;
@ManyToOne
@JoinColumn(name = "departmentId", nullable = false)
private DepartmentEntity department;
public EntrysDataEntity() {
super();
}
public EntrysDataEntity(Long id, String login, String password, String role, DepartmentEntity department) {
super(id);
public EntrysDataEntity(String login, String password, String role, DepartmentEntity department) {
this.login = login;
this.password = password;
this.role = role;

View File

@ -1,10 +1,16 @@
package com.example.demo.entrysData.repository;
import java.util.List;
import java.util.Optional;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.example.demo.core.repository.MapRepository;
import com.example.demo.entrysData.model.EntrysDataEntity;
@Repository
public class EntrysDataRepository extends MapRepository<EntrysDataEntity> {
public interface EntrysDataRepository extends CrudRepository<EntrysDataEntity, Long> {
Optional<EntrysDataEntity> findByloginIgnoreCase(String name);
List<EntrysDataEntity> findByDepartmentId(long departmentId);
}

View File

@ -1,9 +1,10 @@
package com.example.demo.entrysData.service;
import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.entrysData.model.EntrysDataEntity;
@ -17,30 +18,52 @@ public class EntrysDataService {
this.repository = repository;
}
public List<EntrysDataEntity> getAll() {
return repository.getAll();
private void checklogin(String name) {
if (repository.findByloginIgnoreCase(name).isPresent()) {
throw new IllegalArgumentException(
String.format("Type with name %s is already exists", name));
}
}
@Transactional(readOnly = true)
public List<EntrysDataEntity> getAll(Long departmentId) {
if (departmentId <= 0L) {
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
} else {
return repository.findByDepartmentId(departmentId);
}
}
@Transactional(readOnly = true)
public EntrysDataEntity get(Long id) {
return Optional.ofNullable(repository.get(id))
.orElseThrow(() -> new NotFoundException(id));
return repository.findById(id)
.orElseThrow(() -> new NotFoundException(EntrysDataEntity.class, id));
}
@Transactional
public EntrysDataEntity create(EntrysDataEntity entity) {
return repository.create(entity);
if (entity == null) {
throw new IllegalArgumentException("Entity is null");
}
checklogin(entity.getLogin());
return repository.save(entity);
}
@Transactional
public EntrysDataEntity update(Long id, EntrysDataEntity entity) {
final EntrysDataEntity existsEntity = get(id);
checklogin(entity.getLogin());
existsEntity.setLogin(entity.getLogin());
existsEntity.setPassword(entity.getPassword());
existsEntity.setDepartment(entity.getDepartment());
return repository.update(existsEntity);
return repository.save(existsEntity);
}
@Transactional
public EntrysDataEntity delete(Long id) {
final EntrysDataEntity existsEntity = get(id);
return repository.delete(existsEntity);
repository.delete(existsEntity);
return existsEntity;
}
public Boolean AuthPassword(Long id, String pas) {
@ -58,6 +81,6 @@ public class EntrysDataService {
if (truePas.equals(oldPas) && !oldPas.equals(newPas)) {
existsEntity.setPassword(newPas);
}
return repository.update(existsEntity);
return repository.save(existsEntity);
}
}

View File

@ -6,6 +6,7 @@ import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
public class NewsDto {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Long id;
@NotNull
private String name;
@ -17,7 +18,6 @@ public class NewsDto {
@Min(1)
private Long departmentId;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Long getId() {
return id;
}

View File

@ -5,18 +5,29 @@ import java.util.Objects;
import com.example.demo.core.model.BaseEntity;
import com.example.demo.department.model.DepartmentEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
@Entity
@Table(name = "news")
public class NewsEntity extends BaseEntity {
@Column(nullable = false, unique = false, length = 10)
private String date;
@Column(nullable = false, unique = false, length = 15)
private String name;
@Column(nullable = false, unique = false, length = 80)
private String description;
@ManyToOne
@JoinColumn(name = "departmentId", nullable = false)
private DepartmentEntity department;
public NewsEntity() {
super();
}
public NewsEntity(Long id, String date, String name, String description, DepartmentEntity department) {
super(id);
public NewsEntity(String date, String name, String description, DepartmentEntity department) {
this.date = date;
this.name = name;
this.description = description;

View File

@ -1,10 +1,16 @@
package com.example.demo.news.repository;
import java.util.List;
import java.util.Optional;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.example.demo.core.repository.MapRepository;
import com.example.demo.news.model.NewsEntity;
@Repository
public class NewsRepository extends MapRepository<NewsEntity> {
public interface NewsRepository extends CrudRepository<NewsEntity, Long> {
Optional<NewsEntity> findByNameIgnoreCase(String name);
List<NewsEntity> findByDepartmentId(long departmentId);
}

View File

@ -1,9 +1,9 @@
package com.example.demo.news.service;
import java.util.List;
import java.util.Optional;
import java.util.Objects;
import java.util.stream.StreamSupport;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.news.model.NewsEntity;
@ -17,35 +17,49 @@ public class NewsService {
this.repository = repository;
}
public List<NewsEntity> getAll(Long departmentId) {
if (Objects.equals(departmentId, 0L)) {
return repository.getAll();
private void checkName(String name) {
if (repository.findByNameIgnoreCase(name).isPresent()) {
throw new IllegalArgumentException(
String.format("Type with name %s is already exists", name));
}
return repository.getAll().stream()
.filter(item -> item.getDepartment().getId().equals(departmentId))
.toList();
}
@Transactional(readOnly = true)
public List<NewsEntity> getAll(Long departmentId) {
if (departmentId <= 0L) {
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
} else {
return repository.findByDepartmentId(departmentId);
}
}
@Transactional(readOnly = true)
public NewsEntity get(Long id) {
return Optional.ofNullable(repository.get(id))
.orElseThrow(() -> new NotFoundException(id));
return repository.findById(id)
.orElseThrow(() -> new NotFoundException(NewsEntity.class, id));
}
@Transactional
public NewsEntity create(NewsEntity entity) {
return repository.create(entity);
checkName(entity.getName());
return repository.save(entity);
}
@Transactional
public NewsEntity update(Long id, NewsEntity entity) {
final NewsEntity existsEntity = get(id);
checkName(entity.getName());
existsEntity.setDate(entity.getDate());
existsEntity.setName(entity.getName());
existsEntity.setDescription(entity.getDescription());
existsEntity.setDepartment(entity.getDepartment());
return repository.update(existsEntity);
return repository.save(existsEntity);
}
@Transactional
public NewsEntity delete(Long id) {
final NewsEntity existsEntity = get(id);
return repository.delete(existsEntity);
repository.delete(existsEntity);
return existsEntity;
}
}

View File

@ -1,12 +1,14 @@
package com.example.demo.java;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
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 org.springframework.dao.DataIntegrityViolationException;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.department.model.DepartmentEntity;
@ -18,29 +20,52 @@ class DepartmentsTests {
@Autowired
private DepartmentService departmentService;
private DepartmentEntity department;
@BeforeEach
void createData() {
removeData();
department = departmentService.create(new DepartmentEntity("ИСТ"));
departmentService.create(new DepartmentEntity("ГУМ"));
departmentService.create(new DepartmentEntity("ИЯ"));
}
@AfterEach
void removeData() {
departmentService.getAll().forEach(item -> departmentService.delete(item.getId()));
}
@Test
void getTest() {
Assertions.assertThrows(NotFoundException.class, () -> departmentService.get(0L));
}
@Test
@Order(1)
void createTest() {
departmentService.create(new DepartmentEntity(null, "test1"));
departmentService.create(new DepartmentEntity(null, "test2"));
final DepartmentEntity last = departmentService.create(new DepartmentEntity(null, "test3"));
Assertions.assertEquals(3, departmentService.getAll().size());
Assertions.assertEquals(last, departmentService.get(3L));
Assertions.assertEquals(department, departmentService.get(department.getId()));
}
@Test
void createNotUniqueTest() {
final DepartmentEntity nonUniquedepartment = new DepartmentEntity("гум");
Assertions.assertThrows(IllegalArgumentException.class, () -> departmentService.create(nonUniquedepartment));
}
@Test
void createNullableTest() {
final DepartmentEntity nullabledepartment = new DepartmentEntity(null);
Assertions.assertThrows(DataIntegrityViolationException.class,
() -> departmentService.create(nullabledepartment));
}
@Test
@Order(2)
void updateTest() {
final String testName = "TESTNAME";
final DepartmentEntity entity = departmentService.get(3L);
final String oldName = entity.getName();
final DepartmentEntity newEntity = departmentService.update(3L,
new DepartmentEntity(1L, testName));
final String oldName = department.getName();
final DepartmentEntity newEntity = departmentService.update(department.getId(),
new DepartmentEntity(testName));
Assertions.assertEquals(3, departmentService.getAll().size());
Assertions.assertEquals(newEntity, departmentService.get(3L));
@ -49,7 +74,6 @@ class DepartmentsTests {
}
@Test
@Order(3)
void deleteTest() {
departmentService.delete(3L);
Assertions.assertEquals(2, departmentService.getAll().size());
@ -57,7 +81,7 @@ class DepartmentsTests {
Assertions.assertEquals(2L, last.getId());
final DepartmentEntity newEntity = departmentService
.create(new DepartmentEntity(null, "testtt"));
.create(new DepartmentEntity("testtt"));
Assertions.assertEquals(3, departmentService.getAll().size());
Assertions.assertEquals(4L, newEntity.getId());
}

View File

@ -34,7 +34,7 @@ class DirectionsTests {
"Русский язык"));
final DirectionsEntity last = directionsService.create(new DirectionsEntity(null, "09.03.03",
"АиСД", new DepartmentEntity(null, "Гум"), "Математика"));
Assertions.assertEquals(3, directionsService.getAll().size());
Assertions.assertEquals(3, directionsService.getAll(0L).size());
Assertions.assertEquals(last, directionsService.get(3L));
}
@ -52,7 +52,7 @@ class DirectionsTests {
final String oldThin = entity.getThings();
final DirectionsEntity newEntity = directionsService.update(3L,
new DirectionsEntity(1L, testCode, testName, testDep, testThin));
Assertions.assertEquals(3, directionsService.getAll().size());
Assertions.assertEquals(3, directionsService.getAll(0L).size());
Assertions.assertEquals(newEntity, directionsService.get(3L));
Assertions.assertEquals(testCode, newEntity.getCode());
Assertions.assertNotEquals(oldCode, newEntity.getCode());
@ -71,14 +71,14 @@ class DirectionsTests {
@Order(3)
void deleteTest() {
directionsService.delete(3L);
Assertions.assertEquals(2, directionsService.getAll().size());
Assertions.assertEquals(2, directionsService.getAll(0L).size());
final DirectionsEntity last = directionsService.get(2L);
Assertions.assertEquals(2L, last.getId());
final DirectionsEntity newEntity = directionsService
.create(new DirectionsEntity(null, "test1", "test2",
new DepartmentEntity(null, "test3"), "test4"));
Assertions.assertEquals(3, directionsService.getAll().size());
Assertions.assertEquals(3, directionsService.getAll(0L).size());
Assertions.assertEquals(4L, newEntity.getId());
}
}

View File

@ -33,7 +33,7 @@ class EntrysDataTests {
new DepartmentEntity(null, "ИСТ")));
final EntrysDataEntity last = entrysDataService.create(new EntrysDataEntity(null, "user3", "12345", "user",
new DepartmentEntity(null, "Гум")));
Assertions.assertEquals(3, entrysDataService.getAll().size());
Assertions.assertEquals(3, entrysDataService.getAll(0L).size());
Assertions.assertEquals(last, entrysDataService.get(3L));
}
@ -49,7 +49,7 @@ class EntrysDataTests {
final DepartmentEntity oldDep = entity.getDepartment();
final EntrysDataEntity newEntity = entrysDataService.update(3L,
new EntrysDataEntity(1L, testLog, testPas, entity.getRole(), testDep));
Assertions.assertEquals(3, entrysDataService.getAll().size());
Assertions.assertEquals(3, entrysDataService.getAll(0L).size());
Assertions.assertEquals(newEntity, entrysDataService.get(3L));
Assertions.assertEquals(testLog, newEntity.getLogin());
@ -66,14 +66,14 @@ class EntrysDataTests {
@Order(3)
void deleteTest() {
entrysDataService.delete(3L);
Assertions.assertEquals(2, entrysDataService.getAll().size());
Assertions.assertEquals(2, entrysDataService.getAll(0L).size());
final EntrysDataEntity last = entrysDataService.get(2L);
Assertions.assertEquals(2L, last.getId());
final EntrysDataEntity newEntity = entrysDataService
.create(new EntrysDataEntity(null, "user4", "12345", "user",
new DepartmentEntity(null, "testtt")));
Assertions.assertEquals(3, entrysDataService.getAll().size());
Assertions.assertEquals(3, entrysDataService.getAll(0L).size());
Assertions.assertEquals(4L, newEntity.getId());
}