Сделанные сущности. Необходимо починить тесты и DemoApplication
This commit is contained in:
parent
abd50a63ed
commit
c4eb1070e1
data.mv.db
src
main/java/com/example/demo
department
directions
api
model
repository
service
entrysData
api
model
repository
service
news
test/java/com/example/demo/java
BIN
data.mv.db
Normal file
BIN
data.mv.db
Normal file
Binary file not shown.
@ -5,11 +5,11 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
|||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
|
||||||
public class DepartmentDto {
|
public class DepartmentDto {
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
private Long id;
|
private Long id;
|
||||||
@NotNull
|
@NotNull
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ public class DepartmentEntity extends BaseEntity {
|
|||||||
public DepartmentEntity() {
|
public DepartmentEntity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public DepartmentEntity(Long id, String name) {
|
public DepartmentEntity(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.PostMapping;
|
|||||||
import org.springframework.web.bind.annotation.PutMapping;
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
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.RestController;
|
||||||
|
|
||||||
import com.example.demo.core.configuration.Constants;
|
import com.example.demo.core.configuration.Constants;
|
||||||
@ -44,8 +45,8 @@ public class DirectionsController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public List<DirectionsDto> getAll() {
|
public List<DirectionsDto> getAll(@RequestParam(name = "departmentId", defaultValue = "0") Long departmentId) {
|
||||||
return directionsService.getAll().stream().map(this::toDto).toList();
|
return directionsService.getAll(departmentId).stream().map(this::toDto).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
|
@ -7,6 +7,7 @@ import jakarta.validation.constraints.NotBlank;
|
|||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
|
||||||
public class DirectionsDto {
|
public class DirectionsDto {
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
private Long id;
|
private Long id;
|
||||||
@NotBlank
|
@NotBlank
|
||||||
private String code;
|
private String code;
|
||||||
@ -17,7 +18,6 @@ public class DirectionsDto {
|
|||||||
private Long departmentId;
|
private Long departmentId;
|
||||||
private String things;
|
private String things;
|
||||||
|
|
||||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ public class DirectionsEntity extends BaseEntity {
|
|||||||
public DirectionsEntity() {
|
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.code = code;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.department = department;
|
this.department = department;
|
||||||
|
@ -1,31 +1,14 @@
|
|||||||
package com.example.demo.directions.repository;
|
package com.example.demo.directions.repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.Query;
|
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
import com.example.demo.directions.model.DirectionsEntity;
|
import com.example.demo.directions.model.DirectionsEntity;
|
||||||
|
|
||||||
public interface DirectionsRepository extends CrudRepository<DirectionsEntity, Long> {
|
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<DirectionsEntity> findByDepartmentId(long departmentId);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
package com.example.demo.directions.service;
|
package com.example.demo.directions.service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import com.example.demo.core.error.NotFoundException;
|
import com.example.demo.core.error.NotFoundException;
|
||||||
import com.example.demo.directions.model.DirectionsEntity;
|
import com.example.demo.directions.model.DirectionsEntity;
|
||||||
@ -13,34 +14,56 @@ import com.example.demo.directions.repository.DirectionsRepository;
|
|||||||
public class DirectionsService {
|
public class DirectionsService {
|
||||||
private final DirectionsRepository repository;
|
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) {
|
public DirectionsService(DirectionsRepository repository) {
|
||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<DirectionsEntity> getAll() {
|
@Transactional(readOnly = true)
|
||||||
return repository.getAll();
|
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) {
|
public DirectionsEntity get(Long id) {
|
||||||
return Optional.ofNullable(repository.get(id))
|
return repository.findById(id)
|
||||||
.orElseThrow(() -> new NotFoundException(id));
|
.orElseThrow(() -> new NotFoundException(DirectionsEntity.class, id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public DirectionsEntity create(DirectionsEntity entity) {
|
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) {
|
public DirectionsEntity update(Long id, DirectionsEntity entity) {
|
||||||
final DirectionsEntity existsEntity = get(id);
|
final DirectionsEntity existsEntity = get(id);
|
||||||
|
checkName(entity.getName());
|
||||||
existsEntity.setCode(entity.getCode());
|
existsEntity.setCode(entity.getCode());
|
||||||
existsEntity.setName(entity.getName());
|
existsEntity.setName(entity.getName());
|
||||||
existsEntity.setDepartment(entity.getDepartment());
|
existsEntity.setDepartment(entity.getDepartment());
|
||||||
existsEntity.setThings(entity.getThings());
|
existsEntity.setThings(entity.getThings());
|
||||||
return repository.update(existsEntity);
|
return repository.save(existsEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public DirectionsEntity delete(Long id) {
|
public DirectionsEntity delete(Long id) {
|
||||||
final DirectionsEntity existsEntity = get(id);
|
final DirectionsEntity existsEntity = get(id);
|
||||||
return repository.delete(existsEntity);
|
repository.delete(existsEntity);
|
||||||
|
return existsEntity;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,8 +45,8 @@ public class EntrysDataController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public List<EntrysDataDto> getAll() {
|
public List<EntrysDataDto> getAll(@RequestParam(name = "departmentId", defaultValue = "0") Long departmentId) {
|
||||||
return entrysDataService.getAll().stream().map(this::toDto).toList();
|
return entrysDataService.getAll(departmentId).stream().map(this::toDto).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
@ -69,8 +69,8 @@ public class EntrysDataController {
|
|||||||
return toDto(entrysDataService.delete(id));
|
return toDto(entrysDataService.delete(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/{id2}")
|
@PutMapping("/{id}")
|
||||||
public EntrysDataDto updatePassword(@PathVariable(name = "id") Long id,
|
public EntrysDataDto updatePassword(@PathVariable(name = "password") Long id,
|
||||||
@RequestParam(name = "newPassword") String newPas,
|
@RequestParam(name = "newPassword") String newPas,
|
||||||
@RequestParam(name = "oldPassword") String oldPas) {
|
@RequestParam(name = "oldPassword") String oldPas) {
|
||||||
return toDto(entrysDataService.updatePassword(id, newPas, oldPas));
|
return toDto(entrysDataService.updatePassword(id, newPas, oldPas));
|
||||||
|
@ -7,6 +7,7 @@ import jakarta.validation.constraints.Min;
|
|||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
|
||||||
public class EntrysDataDto {
|
public class EntrysDataDto {
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
private Long id;
|
private Long id;
|
||||||
@NotNull
|
@NotNull
|
||||||
@Min(1)
|
@Min(1)
|
||||||
@ -18,7 +19,6 @@ public class EntrysDataDto {
|
|||||||
@NotBlank
|
@NotBlank
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
@ -5,18 +5,29 @@ import java.util.Objects;
|
|||||||
import com.example.demo.core.model.BaseEntity;
|
import com.example.demo.core.model.BaseEntity;
|
||||||
import com.example.demo.department.model.DepartmentEntity;
|
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 {
|
public class EntrysDataEntity extends BaseEntity {
|
||||||
|
@Column(nullable = false, unique = true, length = 30)
|
||||||
private String login;
|
private String login;
|
||||||
|
@Column(nullable = false, unique = false, length = 25)
|
||||||
private String password;
|
private String password;
|
||||||
|
@Column(nullable = false, unique = false, length = 10)
|
||||||
private String role;
|
private String role;
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "departmentId", nullable = false)
|
||||||
private DepartmentEntity department;
|
private DepartmentEntity department;
|
||||||
|
|
||||||
public EntrysDataEntity() {
|
public EntrysDataEntity() {
|
||||||
super();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public EntrysDataEntity(Long id, String login, String password, String role, DepartmentEntity department) {
|
public EntrysDataEntity(String login, String password, String role, DepartmentEntity department) {
|
||||||
super(id);
|
|
||||||
this.login = login;
|
this.login = login;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.role = role;
|
this.role = role;
|
||||||
|
@ -1,10 +1,16 @@
|
|||||||
package com.example.demo.entrysData.repository;
|
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 org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import com.example.demo.core.repository.MapRepository;
|
|
||||||
import com.example.demo.entrysData.model.EntrysDataEntity;
|
import com.example.demo.entrysData.model.EntrysDataEntity;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public class EntrysDataRepository extends MapRepository<EntrysDataEntity> {
|
public interface EntrysDataRepository extends CrudRepository<EntrysDataEntity, Long> {
|
||||||
|
Optional<EntrysDataEntity> findByloginIgnoreCase(String name);
|
||||||
|
|
||||||
|
List<EntrysDataEntity> findByDepartmentId(long departmentId);
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
package com.example.demo.entrysData.service;
|
package com.example.demo.entrysData.service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import com.example.demo.core.error.NotFoundException;
|
import com.example.demo.core.error.NotFoundException;
|
||||||
import com.example.demo.entrysData.model.EntrysDataEntity;
|
import com.example.demo.entrysData.model.EntrysDataEntity;
|
||||||
@ -17,30 +18,52 @@ public class EntrysDataService {
|
|||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<EntrysDataEntity> getAll() {
|
private void checklogin(String name) {
|
||||||
return repository.getAll();
|
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) {
|
public EntrysDataEntity get(Long id) {
|
||||||
return Optional.ofNullable(repository.get(id))
|
return repository.findById(id)
|
||||||
.orElseThrow(() -> new NotFoundException(id));
|
.orElseThrow(() -> new NotFoundException(EntrysDataEntity.class, id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public EntrysDataEntity create(EntrysDataEntity entity) {
|
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) {
|
public EntrysDataEntity update(Long id, EntrysDataEntity entity) {
|
||||||
final EntrysDataEntity existsEntity = get(id);
|
final EntrysDataEntity existsEntity = get(id);
|
||||||
|
checklogin(entity.getLogin());
|
||||||
existsEntity.setLogin(entity.getLogin());
|
existsEntity.setLogin(entity.getLogin());
|
||||||
existsEntity.setPassword(entity.getPassword());
|
existsEntity.setPassword(entity.getPassword());
|
||||||
existsEntity.setDepartment(entity.getDepartment());
|
existsEntity.setDepartment(entity.getDepartment());
|
||||||
return repository.update(existsEntity);
|
return repository.save(existsEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public EntrysDataEntity delete(Long id) {
|
public EntrysDataEntity delete(Long id) {
|
||||||
final EntrysDataEntity existsEntity = get(id);
|
final EntrysDataEntity existsEntity = get(id);
|
||||||
return repository.delete(existsEntity);
|
repository.delete(existsEntity);
|
||||||
|
return existsEntity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boolean AuthPassword(Long id, String pas) {
|
public Boolean AuthPassword(Long id, String pas) {
|
||||||
@ -58,6 +81,6 @@ public class EntrysDataService {
|
|||||||
if (truePas.equals(oldPas) && !oldPas.equals(newPas)) {
|
if (truePas.equals(oldPas) && !oldPas.equals(newPas)) {
|
||||||
existsEntity.setPassword(newPas);
|
existsEntity.setPassword(newPas);
|
||||||
}
|
}
|
||||||
return repository.update(existsEntity);
|
return repository.save(existsEntity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import jakarta.validation.constraints.Min;
|
|||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
|
||||||
public class NewsDto {
|
public class NewsDto {
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
private Long id;
|
private Long id;
|
||||||
@NotNull
|
@NotNull
|
||||||
private String name;
|
private String name;
|
||||||
@ -17,7 +18,6 @@ public class NewsDto {
|
|||||||
@Min(1)
|
@Min(1)
|
||||||
private Long departmentId;
|
private Long departmentId;
|
||||||
|
|
||||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
@ -5,18 +5,29 @@ import java.util.Objects;
|
|||||||
import com.example.demo.core.model.BaseEntity;
|
import com.example.demo.core.model.BaseEntity;
|
||||||
import com.example.demo.department.model.DepartmentEntity;
|
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 {
|
public class NewsEntity extends BaseEntity {
|
||||||
|
@Column(nullable = false, unique = false, length = 10)
|
||||||
private String date;
|
private String date;
|
||||||
|
@Column(nullable = false, unique = false, length = 15)
|
||||||
private String name;
|
private String name;
|
||||||
|
@Column(nullable = false, unique = false, length = 80)
|
||||||
private String description;
|
private String description;
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "departmentId", nullable = false)
|
||||||
private DepartmentEntity department;
|
private DepartmentEntity department;
|
||||||
|
|
||||||
public NewsEntity() {
|
public NewsEntity() {
|
||||||
super();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public NewsEntity(Long id, String date, String name, String description, DepartmentEntity department) {
|
public NewsEntity(String date, String name, String description, DepartmentEntity department) {
|
||||||
super(id);
|
|
||||||
this.date = date;
|
this.date = date;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
|
@ -1,10 +1,16 @@
|
|||||||
package com.example.demo.news.repository;
|
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 org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import com.example.demo.core.repository.MapRepository;
|
|
||||||
import com.example.demo.news.model.NewsEntity;
|
import com.example.demo.news.model.NewsEntity;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public class NewsRepository extends MapRepository<NewsEntity> {
|
public interface NewsRepository extends CrudRepository<NewsEntity, Long> {
|
||||||
|
Optional<NewsEntity> findByNameIgnoreCase(String name);
|
||||||
|
|
||||||
|
List<NewsEntity> findByDepartmentId(long departmentId);
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
package com.example.demo.news.service;
|
package com.example.demo.news.service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.stream.StreamSupport;
|
||||||
import java.util.Objects;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import com.example.demo.core.error.NotFoundException;
|
import com.example.demo.core.error.NotFoundException;
|
||||||
import com.example.demo.news.model.NewsEntity;
|
import com.example.demo.news.model.NewsEntity;
|
||||||
@ -17,35 +17,49 @@ public class NewsService {
|
|||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<NewsEntity> getAll(Long departmentId) {
|
private void checkName(String name) {
|
||||||
if (Objects.equals(departmentId, 0L)) {
|
if (repository.findByNameIgnoreCase(name).isPresent()) {
|
||||||
return repository.getAll();
|
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) {
|
public NewsEntity get(Long id) {
|
||||||
return Optional.ofNullable(repository.get(id))
|
return repository.findById(id)
|
||||||
.orElseThrow(() -> new NotFoundException(id));
|
.orElseThrow(() -> new NotFoundException(NewsEntity.class, id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public NewsEntity create(NewsEntity entity) {
|
public NewsEntity create(NewsEntity entity) {
|
||||||
return repository.create(entity);
|
checkName(entity.getName());
|
||||||
|
return repository.save(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public NewsEntity update(Long id, NewsEntity entity) {
|
public NewsEntity update(Long id, NewsEntity entity) {
|
||||||
final NewsEntity existsEntity = get(id);
|
final NewsEntity existsEntity = get(id);
|
||||||
|
checkName(entity.getName());
|
||||||
existsEntity.setDate(entity.getDate());
|
existsEntity.setDate(entity.getDate());
|
||||||
existsEntity.setName(entity.getName());
|
existsEntity.setName(entity.getName());
|
||||||
existsEntity.setDescription(entity.getDescription());
|
existsEntity.setDescription(entity.getDescription());
|
||||||
existsEntity.setDepartment(entity.getDepartment());
|
existsEntity.setDepartment(entity.getDepartment());
|
||||||
return repository.update(existsEntity);
|
return repository.save(existsEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public NewsEntity delete(Long id) {
|
public NewsEntity delete(Long id) {
|
||||||
final NewsEntity existsEntity = get(id);
|
final NewsEntity existsEntity = get(id);
|
||||||
return repository.delete(existsEntity);
|
repository.delete(existsEntity);
|
||||||
|
return existsEntity;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
package com.example.demo.java;
|
package com.example.demo.java;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||||
import org.junit.jupiter.api.Order;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.TestMethodOrder;
|
import org.junit.jupiter.api.TestMethodOrder;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
|
|
||||||
import com.example.demo.core.error.NotFoundException;
|
import com.example.demo.core.error.NotFoundException;
|
||||||
import com.example.demo.department.model.DepartmentEntity;
|
import com.example.demo.department.model.DepartmentEntity;
|
||||||
@ -18,29 +20,52 @@ class DepartmentsTests {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private DepartmentService departmentService;
|
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
|
@Test
|
||||||
void getTest() {
|
void getTest() {
|
||||||
Assertions.assertThrows(NotFoundException.class, () -> departmentService.get(0L));
|
Assertions.assertThrows(NotFoundException.class, () -> departmentService.get(0L));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Order(1)
|
|
||||||
void createTest() {
|
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(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
|
@Test
|
||||||
@Order(2)
|
|
||||||
void updateTest() {
|
void updateTest() {
|
||||||
final String testName = "TESTNAME";
|
final String testName = "TESTNAME";
|
||||||
final DepartmentEntity entity = departmentService.get(3L);
|
final String oldName = department.getName();
|
||||||
final String oldName = entity.getName();
|
final DepartmentEntity newEntity = departmentService.update(department.getId(),
|
||||||
final DepartmentEntity newEntity = departmentService.update(3L,
|
new DepartmentEntity(testName));
|
||||||
new DepartmentEntity(1L, testName));
|
|
||||||
Assertions.assertEquals(3, departmentService.getAll().size());
|
Assertions.assertEquals(3, departmentService.getAll().size());
|
||||||
Assertions.assertEquals(newEntity, departmentService.get(3L));
|
Assertions.assertEquals(newEntity, departmentService.get(3L));
|
||||||
|
|
||||||
@ -49,7 +74,6 @@ class DepartmentsTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Order(3)
|
|
||||||
void deleteTest() {
|
void deleteTest() {
|
||||||
departmentService.delete(3L);
|
departmentService.delete(3L);
|
||||||
Assertions.assertEquals(2, departmentService.getAll().size());
|
Assertions.assertEquals(2, departmentService.getAll().size());
|
||||||
@ -57,7 +81,7 @@ class DepartmentsTests {
|
|||||||
Assertions.assertEquals(2L, last.getId());
|
Assertions.assertEquals(2L, last.getId());
|
||||||
|
|
||||||
final DepartmentEntity newEntity = departmentService
|
final DepartmentEntity newEntity = departmentService
|
||||||
.create(new DepartmentEntity(null, "testtt"));
|
.create(new DepartmentEntity("testtt"));
|
||||||
Assertions.assertEquals(3, departmentService.getAll().size());
|
Assertions.assertEquals(3, departmentService.getAll().size());
|
||||||
Assertions.assertEquals(4L, newEntity.getId());
|
Assertions.assertEquals(4L, newEntity.getId());
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ class DirectionsTests {
|
|||||||
"Русский язык"));
|
"Русский язык"));
|
||||||
final DirectionsEntity last = directionsService.create(new DirectionsEntity(null, "09.03.03",
|
final DirectionsEntity last = directionsService.create(new DirectionsEntity(null, "09.03.03",
|
||||||
"АиСД", new DepartmentEntity(null, "Гум"), "Математика"));
|
"АиСД", new DepartmentEntity(null, "Гум"), "Математика"));
|
||||||
Assertions.assertEquals(3, directionsService.getAll().size());
|
Assertions.assertEquals(3, directionsService.getAll(0L).size());
|
||||||
Assertions.assertEquals(last, directionsService.get(3L));
|
Assertions.assertEquals(last, directionsService.get(3L));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,7 +52,7 @@ class DirectionsTests {
|
|||||||
final String oldThin = entity.getThings();
|
final String oldThin = entity.getThings();
|
||||||
final DirectionsEntity newEntity = directionsService.update(3L,
|
final DirectionsEntity newEntity = directionsService.update(3L,
|
||||||
new DirectionsEntity(1L, testCode, testName, testDep, testThin));
|
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(newEntity, directionsService.get(3L));
|
||||||
Assertions.assertEquals(testCode, newEntity.getCode());
|
Assertions.assertEquals(testCode, newEntity.getCode());
|
||||||
Assertions.assertNotEquals(oldCode, newEntity.getCode());
|
Assertions.assertNotEquals(oldCode, newEntity.getCode());
|
||||||
@ -71,14 +71,14 @@ class DirectionsTests {
|
|||||||
@Order(3)
|
@Order(3)
|
||||||
void deleteTest() {
|
void deleteTest() {
|
||||||
directionsService.delete(3L);
|
directionsService.delete(3L);
|
||||||
Assertions.assertEquals(2, directionsService.getAll().size());
|
Assertions.assertEquals(2, directionsService.getAll(0L).size());
|
||||||
final DirectionsEntity last = directionsService.get(2L);
|
final DirectionsEntity last = directionsService.get(2L);
|
||||||
Assertions.assertEquals(2L, last.getId());
|
Assertions.assertEquals(2L, last.getId());
|
||||||
|
|
||||||
final DirectionsEntity newEntity = directionsService
|
final DirectionsEntity newEntity = directionsService
|
||||||
.create(new DirectionsEntity(null, "test1", "test2",
|
.create(new DirectionsEntity(null, "test1", "test2",
|
||||||
new DepartmentEntity(null, "test3"), "test4"));
|
new DepartmentEntity(null, "test3"), "test4"));
|
||||||
Assertions.assertEquals(3, directionsService.getAll().size());
|
Assertions.assertEquals(3, directionsService.getAll(0L).size());
|
||||||
Assertions.assertEquals(4L, newEntity.getId());
|
Assertions.assertEquals(4L, newEntity.getId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ class EntrysDataTests {
|
|||||||
new DepartmentEntity(null, "ИСТ")));
|
new DepartmentEntity(null, "ИСТ")));
|
||||||
final EntrysDataEntity last = entrysDataService.create(new EntrysDataEntity(null, "user3", "12345", "user",
|
final EntrysDataEntity last = entrysDataService.create(new EntrysDataEntity(null, "user3", "12345", "user",
|
||||||
new DepartmentEntity(null, "Гум")));
|
new DepartmentEntity(null, "Гум")));
|
||||||
Assertions.assertEquals(3, entrysDataService.getAll().size());
|
Assertions.assertEquals(3, entrysDataService.getAll(0L).size());
|
||||||
Assertions.assertEquals(last, entrysDataService.get(3L));
|
Assertions.assertEquals(last, entrysDataService.get(3L));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,7 +49,7 @@ class EntrysDataTests {
|
|||||||
final DepartmentEntity oldDep = entity.getDepartment();
|
final DepartmentEntity oldDep = entity.getDepartment();
|
||||||
final EntrysDataEntity newEntity = entrysDataService.update(3L,
|
final EntrysDataEntity newEntity = entrysDataService.update(3L,
|
||||||
new EntrysDataEntity(1L, testLog, testPas, entity.getRole(), testDep));
|
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(newEntity, entrysDataService.get(3L));
|
||||||
|
|
||||||
Assertions.assertEquals(testLog, newEntity.getLogin());
|
Assertions.assertEquals(testLog, newEntity.getLogin());
|
||||||
@ -66,14 +66,14 @@ class EntrysDataTests {
|
|||||||
@Order(3)
|
@Order(3)
|
||||||
void deleteTest() {
|
void deleteTest() {
|
||||||
entrysDataService.delete(3L);
|
entrysDataService.delete(3L);
|
||||||
Assertions.assertEquals(2, entrysDataService.getAll().size());
|
Assertions.assertEquals(2, entrysDataService.getAll(0L).size());
|
||||||
final EntrysDataEntity last = entrysDataService.get(2L);
|
final EntrysDataEntity last = entrysDataService.get(2L);
|
||||||
Assertions.assertEquals(2L, last.getId());
|
Assertions.assertEquals(2L, last.getId());
|
||||||
|
|
||||||
final EntrysDataEntity newEntity = entrysDataService
|
final EntrysDataEntity newEntity = entrysDataService
|
||||||
.create(new EntrysDataEntity(null, "user4", "12345", "user",
|
.create(new EntrysDataEntity(null, "user4", "12345", "user",
|
||||||
new DepartmentEntity(null, "testtt")));
|
new DepartmentEntity(null, "testtt")));
|
||||||
Assertions.assertEquals(3, entrysDataService.getAll().size());
|
Assertions.assertEquals(3, entrysDataService.getAll(0L).size());
|
||||||
Assertions.assertEquals(4L, newEntity.getId());
|
Assertions.assertEquals(4L, newEntity.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user