Лабораторная 3 (Backend)
This commit is contained in:
parent
b224f1b8a1
commit
189d0ed203
@ -1,77 +0,0 @@
|
|||||||
package com.example.demo;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.boot.CommandLineRunner;
|
|
||||||
import org.springframework.boot.SpringApplication;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import com.example.demo.house.model.HouseEntity;
|
|
||||||
import com.example.demo.house.service.HouseService;
|
|
||||||
import com.example.demo.types.model.TypeEntity;
|
|
||||||
import com.example.demo.types.service.TypeService;
|
|
||||||
import com.example.demo.users.model.UserEntity;
|
|
||||||
import com.example.demo.users.service.UsersService;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/api")
|
|
||||||
public class DemoApplication implements CommandLineRunner {
|
|
||||||
private final Logger log = LoggerFactory.getLogger(DemoApplication.class);
|
|
||||||
|
|
||||||
private final TypeService typeService;
|
|
||||||
private final HouseService houseService;
|
|
||||||
private final UsersService usersService;
|
|
||||||
|
|
||||||
public DemoApplication(TypeService typeService, HouseService houseService, UsersService usersService) {
|
|
||||||
this.typeService = typeService;
|
|
||||||
this.houseService = houseService;
|
|
||||||
this.usersService = usersService;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
SpringApplication.run(DemoApplication.class, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run(String... args) throws Exception {
|
|
||||||
log.info("Create default types values");
|
|
||||||
final var type1 = typeService.create(new TypeEntity(null, "Апартаменты"));
|
|
||||||
final var type2 = typeService.create(new TypeEntity(null, "Таунхауз"));
|
|
||||||
final var type3 = typeService.create(new TypeEntity(null, "Пентхауз"));
|
|
||||||
final var type4 = typeService.create(new TypeEntity(null, "Квартира"));
|
|
||||||
|
|
||||||
final var user1 = usersService.create(new UserEntity(null, "Иван",
|
|
||||||
"ivan@mail.ru", "+79048756587", "1"));
|
|
||||||
final var user2 = usersService.create(new UserEntity(null, "Пётр",
|
|
||||||
"ivan@mail.ru", "+79048756587", "1232"));
|
|
||||||
|
|
||||||
log.info("Create default items values");
|
|
||||||
|
|
||||||
houseService.create(user1.getId(), new HouseEntity(null, type4, user1, "Квартира1",
|
|
||||||
"Удобная квартира в центре города", 49999.00));
|
|
||||||
houseService.create(user1.getId(), new HouseEntity(null, type1, user1, "Квартира2",
|
|
||||||
"Удобная квартира на севере", 129999.00));
|
|
||||||
houseService.create(user1.getId(), new HouseEntity(null, type4, user1, "Квартира3",
|
|
||||||
"Квартира с большой парковкой", 15450.50));
|
|
||||||
houseService.create(user2.getId(), new HouseEntity(null, type2, user2, "Апартаменты",
|
|
||||||
"Апартаменты на 24 этаже", 69900.50));
|
|
||||||
houseService.create(user2.getId(), new HouseEntity(null, type2, user2, "Пентауз",
|
|
||||||
"Дорогой пентхауз", 150000.00));
|
|
||||||
houseService.create(user1.getId(), new HouseEntity(null, type3, user1, "Таунхауз",
|
|
||||||
"Большой бассейн", 75000.00));
|
|
||||||
houseService.create(user2.getId(), new HouseEntity(null, type3, user2, "Пентауз",
|
|
||||||
"Для каждого!", 67800.00));
|
|
||||||
|
|
||||||
usersService.create(new UserEntity(null, "Иван",
|
|
||||||
"ivan@mail.ru", "+79048756587", "1"));
|
|
||||||
usersService.create(new UserEntity(null, "Пётр",
|
|
||||||
"petr@mail.ru", "89057655257", "12345"));
|
|
||||||
usersService.create(new UserEntity(null, "Сергей",
|
|
||||||
"serg79@mail.ru", "95-85-72", "1234"));
|
|
||||||
usersService.create(new UserEntity(null, "Джеймс",
|
|
||||||
"jaims21@mail.ru", "89047856987", "jaen12"));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
package com.example.demo.core.error;
|
|
||||||
|
|
||||||
public class NotFoundException extends RuntimeException {
|
|
||||||
public NotFoundException(Long id) {
|
|
||||||
super(String.format("Entity with id [%s] is not found or not exists", id));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
package com.example.demo.core.model;
|
|
||||||
|
|
||||||
public abstract class BaseEntity {
|
|
||||||
protected Long id;
|
|
||||||
|
|
||||||
protected BaseEntity() {
|
|
||||||
}
|
|
||||||
|
|
||||||
protected BaseEntity(Long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(Long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
package com.example.demo.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();
|
|
||||||
}
|
|
@ -1,57 +0,0 @@
|
|||||||
package com.example.demo.core.repository;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.TreeMap;
|
|
||||||
|
|
||||||
import com.example.demo.core.model.BaseEntity;
|
|
||||||
|
|
||||||
public abstract class MapRepository<E extends BaseEntity> implements CommonRepository<E, Long> {
|
|
||||||
private final Map<Long, E> entities = new TreeMap<>();
|
|
||||||
private Long lastId = 0L;
|
|
||||||
|
|
||||||
protected MapRepository() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<E> getAll() {
|
|
||||||
return entities.values().stream().toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public E get(Long 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 (get(entity.getId()) == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
entities.put(entity.getId(), entity);
|
|
||||||
return entity;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public E delete(E entity) {
|
|
||||||
if (get(entity.getId()) == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
entities.remove(entity.getId());
|
|
||||||
return entity;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void deleteAll() {
|
|
||||||
lastId = 0L;
|
|
||||||
entities.clear();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
package com.example.demo.house.repository;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
import com.example.demo.core.repository.MapRepository;
|
|
||||||
import com.example.demo.house.model.HouseEntity;
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
public class HouseRepository extends MapRepository<HouseEntity> {
|
|
||||||
|
|
||||||
}
|
|
@ -1,72 +0,0 @@
|
|||||||
package com.example.demo.house.service;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import com.example.demo.core.error.NotFoundException;
|
|
||||||
import com.example.demo.house.model.HouseEntity;
|
|
||||||
import com.example.demo.house.repository.HouseRepository;
|
|
||||||
import com.example.demo.users.model.UserEntity;
|
|
||||||
import com.example.demo.users.service.UsersService;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class HouseService {
|
|
||||||
private final HouseRepository repository;
|
|
||||||
private final UsersService userService;
|
|
||||||
|
|
||||||
public HouseService(HouseRepository repository, UsersService usersService) {
|
|
||||||
this.repository = repository;
|
|
||||||
this.userService = usersService;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<HouseEntity> getAllHouses() {
|
|
||||||
return repository.getAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<HouseEntity> getAll(Long typeId, Long userId) {
|
|
||||||
final UserEntity user = userService.get(userId);
|
|
||||||
if (typeId > 0) {
|
|
||||||
return repository.getAll().stream()
|
|
||||||
.filter(house -> house.getType().getId().equals(typeId))
|
|
||||||
.filter(house -> house.getUser().equals(user))
|
|
||||||
.toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
return repository.getAll().stream()
|
|
||||||
.filter(house -> house.getUser().equals(user))
|
|
||||||
.toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public HouseEntity get(Long id, Long userId) {
|
|
||||||
final UserEntity user = userService.get(userId);
|
|
||||||
final HouseEntity house = Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id));
|
|
||||||
if (!house.getUser().equals(user)) {
|
|
||||||
throw new NotFoundException(id);
|
|
||||||
}
|
|
||||||
return house;
|
|
||||||
}
|
|
||||||
|
|
||||||
public HouseEntity create(Long userId, HouseEntity entity) {
|
|
||||||
final UserEntity user = userService.get(userId);
|
|
||||||
entity.setUser(user);
|
|
||||||
return repository.create(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public HouseEntity update(Long id, Long userId, HouseEntity entity) {
|
|
||||||
userService.get(userId);
|
|
||||||
final HouseEntity existEntity = get(id, userId);
|
|
||||||
existEntity.setTitle(entity.getTitle());
|
|
||||||
existEntity.setDescription(entity.getDescription());
|
|
||||||
existEntity.setPrice(entity.getPrice());
|
|
||||||
existEntity.setType(entity.getType());
|
|
||||||
return repository.update(existEntity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public HouseEntity delete(Long id, Long userId) {
|
|
||||||
userService.get(userId);
|
|
||||||
final HouseEntity existEntity = get(id, userId);
|
|
||||||
return repository.delete(existEntity);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
package com.example.demo.speaker.api;
|
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import com.example.demo.speaker.service.SpeakerService;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
public class SpeakerController {
|
|
||||||
private final SpeakerService speakerService;
|
|
||||||
|
|
||||||
public SpeakerController(SpeakerService speakerService) {
|
|
||||||
this.speakerService = speakerService;
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping
|
|
||||||
public String hello(
|
|
||||||
@RequestParam(value = "name", defaultValue = "Мир") String name,
|
|
||||||
@RequestParam(value = "lang", defaultValue = "ru") String lang) {
|
|
||||||
return speakerService.say(name, lang);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
package com.example.demo.speaker.configuration;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
import com.example.demo.speaker.domain.Speaker;
|
|
||||||
import com.example.demo.speaker.domain.SpeakerEng;
|
|
||||||
import com.example.demo.speaker.domain.SpeakerRus;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class SpeakerConfiguration {
|
|
||||||
private final Logger log = LoggerFactory.getLogger(SpeakerConfiguration.class);
|
|
||||||
|
|
||||||
@Bean(value = "ru", initMethod = "init", destroyMethod = "destroy")
|
|
||||||
public SpeakerRus createRusSpeaker() {
|
|
||||||
log.info("Call createRusSpeaker()");
|
|
||||||
return new SpeakerRus();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean(value = "en")
|
|
||||||
public Speaker createEngSpeaker() {
|
|
||||||
log.info("Call createEngSpeaker()");
|
|
||||||
return new SpeakerEng();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
package com.example.demo.speaker.domain;
|
|
||||||
|
|
||||||
public interface Speaker {
|
|
||||||
String say();
|
|
||||||
}
|
|
@ -1,28 +0,0 @@
|
|||||||
package com.example.demo.speaker.domain;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import jakarta.annotation.PostConstruct;
|
|
||||||
import jakarta.annotation.PreDestroy;
|
|
||||||
|
|
||||||
@Component(value = "de")
|
|
||||||
public class SpeakerDeu implements Speaker {
|
|
||||||
private final Logger log = LoggerFactory.getLogger(SpeakerDeu.class);
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String say() {
|
|
||||||
return "Hallo";
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
public void init() {
|
|
||||||
log.info("SpeakerDeu.init()");
|
|
||||||
}
|
|
||||||
|
|
||||||
@PreDestroy
|
|
||||||
public void destroy() {
|
|
||||||
log.info("SpeakerDeu.destroy()");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
package com.example.demo.speaker.domain;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.beans.factory.DisposableBean;
|
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
|
||||||
|
|
||||||
public class SpeakerEng implements Speaker, InitializingBean, DisposableBean {
|
|
||||||
private final Logger log = LoggerFactory.getLogger(SpeakerEng.class);
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String say() {
|
|
||||||
return "Hello";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterPropertiesSet() {
|
|
||||||
log.info("SpeakerEng.afterPropertiesSet()");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void destroy() {
|
|
||||||
log.info("SpeakerEng.destroy()");
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
package com.example.demo.speaker.domain;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
public class SpeakerRus implements Speaker {
|
|
||||||
private final Logger log = LoggerFactory.getLogger(SpeakerRus.class);
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String say() {
|
|
||||||
return "Привет";
|
|
||||||
}
|
|
||||||
|
|
||||||
public void init() {
|
|
||||||
log.info("SpeakerRus.init()");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void destroy() {
|
|
||||||
log.info("SpeakerRus.destroy()");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
package com.example.demo.speaker.service;
|
|
||||||
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import com.example.demo.speaker.domain.Speaker;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class SpeakerService {
|
|
||||||
private final ApplicationContext applicationContext;
|
|
||||||
|
|
||||||
public SpeakerService(ApplicationContext applicationContext) {
|
|
||||||
this.applicationContext = applicationContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String say(String name, String lang) {
|
|
||||||
@SuppressWarnings("null")
|
|
||||||
final Speaker speaker = (Speaker) applicationContext.getBean(lang);
|
|
||||||
return String.format("%s, %s!", speaker.say(), name);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
package com.example.demo.types.repository;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
import com.example.demo.core.repository.MapRepository;
|
|
||||||
import com.example.demo.types.model.TypeEntity;
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
public class TypeRepository extends MapRepository<TypeEntity> {
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
package com.example.demo.types.service;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import com.example.demo.core.error.NotFoundException;
|
|
||||||
import com.example.demo.types.model.TypeEntity;
|
|
||||||
import com.example.demo.types.repository.TypeRepository;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class TypeService {
|
|
||||||
private final TypeRepository repository;
|
|
||||||
|
|
||||||
public TypeService(TypeRepository repository) {
|
|
||||||
this.repository = repository;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<TypeEntity> getAll() {
|
|
||||||
return repository.getAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
public TypeEntity get(Long id) {
|
|
||||||
return Optional.ofNullable(repository.get(id))
|
|
||||||
.orElseThrow(() -> new NotFoundException(id));
|
|
||||||
}
|
|
||||||
|
|
||||||
public TypeEntity create(TypeEntity entity) {
|
|
||||||
return repository.create(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public TypeEntity update(Long id, TypeEntity entity) {
|
|
||||||
final TypeEntity existsEntity = get(id);
|
|
||||||
existsEntity.setName(entity.getName());
|
|
||||||
return repository.update(existsEntity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public TypeEntity delete(Long id) {
|
|
||||||
final TypeEntity existsEntity = get(id);
|
|
||||||
return repository.delete(existsEntity);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,75 +0,0 @@
|
|||||||
package com.example.demo.users.model;
|
|
||||||
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
import com.example.demo.core.model.BaseEntity;
|
|
||||||
|
|
||||||
public class UserEntity extends BaseEntity {
|
|
||||||
private String name;
|
|
||||||
private String email;
|
|
||||||
private String numberphone;
|
|
||||||
private String password;
|
|
||||||
|
|
||||||
public UserEntity() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
public UserEntity(Long id, String name, String email, String numberphone, String password) {
|
|
||||||
super(id);
|
|
||||||
this.name = name;
|
|
||||||
this.email = email;
|
|
||||||
this.numberphone = numberphone;
|
|
||||||
this.password = password;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getEmail() {
|
|
||||||
return email;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setEmail(String email) {
|
|
||||||
this.email = email;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getNumberPhone() {
|
|
||||||
return numberphone;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setNumberPhone(String numberphone) {
|
|
||||||
this.numberphone = numberphone;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPassword() {
|
|
||||||
return password;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPassword(String password) {
|
|
||||||
this.password = password;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return Objects.hash(id, name, email, numberphone, password);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if (this == obj)
|
|
||||||
return true;
|
|
||||||
if (obj == null || getClass() != obj.getClass())
|
|
||||||
return false;
|
|
||||||
final UserEntity other = (UserEntity) obj;
|
|
||||||
return Objects.equals(other.getId(), id)
|
|
||||||
&& Objects.equals(other.getName(), name)
|
|
||||||
&& Objects.equals(other.getEmail(), email)
|
|
||||||
&& Objects.equals(other.getNumberPhone(), numberphone)
|
|
||||||
&& Objects.equals(other.getPassword(), password);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
package com.example.demo.users.repository;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
import com.example.demo.core.repository.MapRepository;
|
|
||||||
import com.example.demo.users.model.UserEntity;
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
public class UsersRepository extends MapRepository<UserEntity> {
|
|
||||||
}
|
|
@ -1,46 +0,0 @@
|
|||||||
package com.example.demo.users.service;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import com.example.demo.core.error.NotFoundException;
|
|
||||||
import com.example.demo.users.model.UserEntity;
|
|
||||||
import com.example.demo.users.repository.UsersRepository;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class UsersService {
|
|
||||||
private final UsersRepository repository;
|
|
||||||
|
|
||||||
public UsersService(UsersRepository repository) {
|
|
||||||
this.repository = repository;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<UserEntity> getAll() {
|
|
||||||
return repository.getAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
public UserEntity get(Long id) {
|
|
||||||
return Optional.ofNullable(repository.get(id))
|
|
||||||
.orElseThrow(() -> new NotFoundException(id));
|
|
||||||
}
|
|
||||||
|
|
||||||
public UserEntity create(UserEntity entity) {
|
|
||||||
return repository.create(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public UserEntity update(Long id, UserEntity entity) {
|
|
||||||
final UserEntity existsEntity = get(id);
|
|
||||||
existsEntity.setName(entity.getName());
|
|
||||||
existsEntity.setEmail(entity.getEmail());
|
|
||||||
existsEntity.setNumberPhone(entity.getNumberPhone());
|
|
||||||
existsEntity.setPassword(entity.getPassword());
|
|
||||||
return repository.update(existsEntity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public UserEntity delete(Long id) {
|
|
||||||
final UserEntity existsEntity = get(id);
|
|
||||||
return repository.delete(existsEntity);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
|
|
@ -1,52 +0,0 @@
|
|||||||
package com.example.demo;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
|
||||||
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 com.example.demo.core.error.NotFoundException;
|
|
||||||
import com.example.demo.house.model.HouseEntity;
|
|
||||||
import com.example.demo.house.service.HouseService;
|
|
||||||
|
|
||||||
@SpringBootTest
|
|
||||||
@TestMethodOrder(OrderAnnotation.class)
|
|
||||||
class HouseServiceTests {
|
|
||||||
@Autowired
|
|
||||||
private HouseService houseService;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getTest() {
|
|
||||||
Assertions.assertThrows(NotFoundException.class, () -> houseService.get(0L, 0L));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(1)
|
|
||||||
void createTest() {
|
|
||||||
houseService.create(1L, new HouseEntity(null, null, null, "fewfe", "cool",210.0));
|
|
||||||
houseService.create(2L, new HouseEntity(null, null, null, "weffew", "cool efwfew", 210.0));
|
|
||||||
houseService.create(3L, new HouseEntity(null, null, null, "efwfew", "very cool wffwe", 2100.0));
|
|
||||||
|
|
||||||
Assertions.assertEquals(5, houseService.getAll(0L, 1L).size());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(2)
|
|
||||||
void updateTest() {
|
|
||||||
final HouseEntity newHouse = new HouseEntity(null, null, null, "House", "cool house", 1500.0);
|
|
||||||
final HouseEntity upHouse = houseService.update(2L, 1L, newHouse);
|
|
||||||
Assertions.assertEquals(newHouse.getTitle(), upHouse.getTitle());
|
|
||||||
Assertions.assertEquals(newHouse.getDescription(), upHouse.getDescription());
|
|
||||||
Assertions.assertEquals(newHouse.getPrice(), upHouse.getPrice());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(3)
|
|
||||||
void deleteTest() {
|
|
||||||
houseService.delete(1L, 1L);
|
|
||||||
Assertions.assertEquals(4, houseService.getAll(0L, 1L).size());
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
package com.example.demo;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
|
|
||||||
import com.example.demo.speaker.service.SpeakerService;
|
|
||||||
|
|
||||||
@SpringBootTest
|
|
||||||
class SpeakerSrviceTests {
|
|
||||||
@Autowired
|
|
||||||
SpeakerService speakerService;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testSpeakerRus() {
|
|
||||||
final String res = speakerService.say("Мир", "ru");
|
|
||||||
Assertions.assertEquals("Привет, Мир!", res);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testSpeakerEng() {
|
|
||||||
final String res = speakerService.say("World", "en");
|
|
||||||
Assertions.assertEquals("Hello, World!", res);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testSpeakerDeu() {
|
|
||||||
final String res = speakerService.say("Welt", "de");
|
|
||||||
Assertions.assertEquals("Hallo, Welt!", res);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testSpeakerErrorWired() {
|
|
||||||
Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> speakerService.say("Мир", "rus"));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,59 +0,0 @@
|
|||||||
package com.example.demo;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
|
||||||
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 com.example.demo.core.error.NotFoundException;
|
|
||||||
import com.example.demo.types.model.TypeEntity;
|
|
||||||
import com.example.demo.types.service.TypeService;
|
|
||||||
|
|
||||||
@SpringBootTest
|
|
||||||
@TestMethodOrder(OrderAnnotation.class)
|
|
||||||
class TypeServiceTests {
|
|
||||||
@Autowired
|
|
||||||
private TypeService typeService;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getTest() {
|
|
||||||
Assertions.assertThrows(NotFoundException.class, () -> typeService.get(0L));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(1)
|
|
||||||
void createTest() {
|
|
||||||
typeService.create(new TypeEntity(null, "Ноутбук"));
|
|
||||||
typeService.create(new TypeEntity(null, "Телефон"));
|
|
||||||
Assertions.assertEquals(6, typeService.getAll().size());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(2)
|
|
||||||
void updateTest() {
|
|
||||||
final String test = "TEST";
|
|
||||||
final TypeEntity entity = typeService.get(3L);
|
|
||||||
final String oldName = entity.getName();
|
|
||||||
final TypeEntity newEntity = typeService.update(3L, new TypeEntity(1L, test));
|
|
||||||
Assertions.assertEquals(6, typeService.getAll().size());
|
|
||||||
Assertions.assertEquals(newEntity, typeService.get(3L));
|
|
||||||
Assertions.assertEquals(test, newEntity.getName());
|
|
||||||
Assertions.assertNotEquals(oldName, newEntity.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(3)
|
|
||||||
void deleteTest() {
|
|
||||||
typeService.delete(3L);
|
|
||||||
Assertions.assertEquals(5, typeService.getAll().size());
|
|
||||||
final TypeEntity last = typeService.get(2L);
|
|
||||||
Assertions.assertEquals(2L, last.getId());
|
|
||||||
|
|
||||||
final TypeEntity newEntity = typeService.create(new TypeEntity(null, "Игровая приставка"));
|
|
||||||
Assertions.assertEquals(6, typeService.getAll().size());
|
|
||||||
Assertions.assertEquals(7, newEntity.getId());
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,59 +0,0 @@
|
|||||||
package com.example.demo;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
|
||||||
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 com.example.demo.core.error.NotFoundException;
|
|
||||||
import com.example.demo.users.model.UserEntity;
|
|
||||||
import com.example.demo.users.service.UsersService;
|
|
||||||
|
|
||||||
@SpringBootTest
|
|
||||||
@TestMethodOrder(OrderAnnotation.class)
|
|
||||||
class UsersServiceTests {
|
|
||||||
@Autowired
|
|
||||||
private UsersService userService;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getTest() {
|
|
||||||
Assertions.assertThrows(NotFoundException.class, () -> userService.get(0L));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(1)
|
|
||||||
void createTest() {
|
|
||||||
userService.create(new UserEntity(null, "Иван", "", "", ""));
|
|
||||||
userService.create(new UserEntity(null, "Иван", "", "", ""));
|
|
||||||
Assertions.assertEquals(8, userService.getAll().size());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(2)
|
|
||||||
void updateTest() {
|
|
||||||
final String test = "TEST";
|
|
||||||
final UserEntity entity = userService.get(3L);
|
|
||||||
final String oldName = entity.getName();
|
|
||||||
final UserEntity newEntity = userService.update(3L, new UserEntity(1L, test, "test", "test", "test"));
|
|
||||||
Assertions.assertEquals(8, userService.getAll().size());
|
|
||||||
Assertions.assertEquals(newEntity, userService.get(3L));
|
|
||||||
Assertions.assertEquals(test, newEntity.getName());
|
|
||||||
Assertions.assertNotEquals(oldName, newEntity.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(3)
|
|
||||||
void deleteTest() {
|
|
||||||
userService.delete(3L);
|
|
||||||
Assertions.assertEquals(7, userService.getAll().size());
|
|
||||||
final UserEntity last = userService.get(2L);
|
|
||||||
Assertions.assertEquals(2L, last.getId());
|
|
||||||
|
|
||||||
final UserEntity newEntity = userService.create(new UserEntity(null, "Игровая приставка", "", "", ""));
|
|
||||||
Assertions.assertEquals(8, userService.getAll().size());
|
|
||||||
Assertions.assertEquals(9, newEntity.getId());
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user