создана базовая сущность, репозитории и тд. (папка core)

This commit is contained in:
ekallin 2024-03-16 14:03:24 +04:00
parent 23a5a3f8c0
commit 776271e081
8 changed files with 138 additions and 1 deletions

View File

@ -19,6 +19,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'
implementation 'org.modelmapper:modelmapper:3.2.0'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

View File

@ -0,0 +1,7 @@
package com.example.backend.categories.model;
import com.example.backend.core.model.BaseEntity;
public class CategorieEntity extends BaseEntity {
}

View File

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

View File

@ -0,0 +1,13 @@
package com.example.backend.core.configurations;
import org.modelmapper.ModelMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MapperConfiguration {
@Bean
ModelMapper modelMapper() {
return new ModelMapper();
}
}

View File

@ -1,4 +1,4 @@
package com.example.backend;
package com.example.backend.core.configurations;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;

View File

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

View File

@ -0,0 +1,19 @@
package com.example.backend.core.repository;
import java.util.List;
public interface CommonRepository<E, T> {
List<E> getAll();
E get(T id);
E create(E entity);
E update(E entity);
E delete(E entity);
void deleteAll();
}

View File

@ -0,0 +1,65 @@
package com.example.backend.core.repository;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import com.example.backend.core.model.BaseEntity;
public abstract class MapRepository<E extends BaseEntity> implements CommonRepository<E, Integer> {
private final Map<Integer, E> entities = new TreeMap<>();
private Integer lastId = 0;
private boolean checkNull(E entity) {
if (get(entity.getId()) == null)
return false;
return true;
}
protected MapRepository() {
}
@Override
public List<E> getAll() {
return entities.values().stream().toList();
}
@Override
public E get(Integer id) {
return entities.get(id);
}
@Override
public E create(E entity) {
lastId++;
entity.setId(lastId);
entities.put(lastId, entity);
return entity;
}
@Override
public E update(E entity) {
if (checkNull(entity)) {
entities.put(lastId, entity);
return entity;
}
return null;
}
@Override
public E delete(E entity) {
if (checkNull(entity)) {
entities.remove(entity.getId());
return entity;
}
return null;
}
@Override
public void deleteAll() {
entities.clear();
lastId = 0;
}
}