создана базовая сущность, репозитории и тд. (папка core)
This commit is contained in:
parent
23a5a3f8c0
commit
776271e081
@ -19,6 +19,8 @@ dependencies {
|
|||||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-actuator'
|
implementation 'org.springframework.boot:spring-boot-starter-actuator'
|
||||||
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'
|
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'
|
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.example.backend.categories.model;
|
||||||
|
|
||||||
|
import com.example.backend.core.model.BaseEntity;
|
||||||
|
|
||||||
|
public class CategorieEntity extends BaseEntity {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.example.backend.core.configurations;
|
||||||
|
|
||||||
|
public class Constants {
|
||||||
|
public static final String API_URL = "/api/1.0";
|
||||||
|
|
||||||
|
private Constants() {
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package com.example.backend;
|
package com.example.backend.core.configurations;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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();
|
||||||
|
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user