изменены классы категорий

This commit is contained in:
ekallin 2024-04-14 23:25:08 +03:00
parent b40fbf0146
commit 957c902e21
4 changed files with 44 additions and 14 deletions

View File

@ -3,14 +3,18 @@ package com.example.backend.categories.api;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
public class CategorieDTO {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Integer id;
@NotBlank
@Size(min = 5, max = 50)
private String name;
@Size(min = 5, max = 50)
private String image;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)

View File

@ -4,9 +4,18 @@ import java.util.Objects;
import com.example.backend.core.model.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
@Entity
@Table(name = "categories")
public class CategorieEntity extends BaseEntity {
@Column(nullable = false, unique = true, length = 15)
private String name;
@Column(nullable = true)
private String image;
public CategorieEntity() {
@ -14,7 +23,6 @@ public class CategorieEntity extends BaseEntity {
}
public CategorieEntity(Integer id, String name, String image) {
super(id);
this.name = name;
this.image = image;
}

View File

@ -1,11 +1,11 @@
package com.example.backend.categories.repository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
import org.springframework.data.repository.CrudRepository;
import com.example.backend.categories.model.CategorieEntity;
import com.example.backend.core.repository.MapRepository;
@Repository
public class CategorieRepository extends MapRepository<CategorieEntity> {
public interface CategorieRepository extends CrudRepository<CategorieEntity, Integer> {
Optional<CategorieEntity> findByNameIgnoreCase(String name);
}

View File

@ -3,11 +3,15 @@ package com.example.backend.categories.service;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service;
import java.util.stream.StreamSupport;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.backend.core.errors.NotFoundException;
import com.example.backend.categories.model.CategorieEntity;
import com.example.backend.categories.repository.CategorieRepository;
import com.example.backend.core.errors.NotFoundException;
@Service
public class CategorieService {
@ -18,28 +22,42 @@ public class CategorieService {
this.repository = repository;
}
public List<CategorieEntity> getAll() {
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));
}
}
@Transactional(readOnly = true)
public List<CategorieEntity> getAll() {
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
}
@Transactional(readOnly = true)
public CategorieEntity get(Integer id) {
return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id));
return repository.findById(id).orElseThrow(() -> new NotFoundException(id));
}
public CategorieEntity create(CategorieEntity entity) {
return repository.create(entity);
if(entity == null){
throw new IllegalArgumentException("Сущность не найдена");
}
checkName(entity.getName());
return repository.save(entity);
}
public CategorieEntity update(Integer id, CategorieEntity entity) {
final CategorieEntity existsentity = get(id);
existsentity.setName(entity.getName());
existsentity.setImage(entity.getImage());
return repository.update(existsentity);
return repository.save(existsentity);
}
public CategorieEntity delete(Integer id) {
final CategorieEntity existsentity = get(id);
return repository.delete(existsentity);
repository.delete(existsentity);
return existsentity;
}
}