что то типо
This commit is contained in:
parent
3fe244e595
commit
15c53ef456
BIN
data.mv.db
Normal file
BIN
data.mv.db
Normal file
Binary file not shown.
@ -1,12 +1,23 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'org.springframework.boot' version '3.2.3'
|
||||
id 'org.springframework.boot' version '3.2.4'
|
||||
id 'io.spring.dependency-management' version '1.1.4'
|
||||
}
|
||||
|
||||
group = 'com.example'
|
||||
version = '0.0.1-SNAPSHOT'
|
||||
|
||||
defaultTasks 'bootRun'
|
||||
|
||||
jar {
|
||||
enabled = false
|
||||
}
|
||||
|
||||
bootJar {
|
||||
archiveFileName = String.format('%s-%s.jar', rootProject.name, version)
|
||||
}
|
||||
|
||||
assert System.properties['java.specification.version'] == '17' || '21'
|
||||
java {
|
||||
sourceCompatibility = '17'
|
||||
}
|
||||
@ -21,6 +32,9 @@ dependencies {
|
||||
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'
|
||||
implementation 'org.modelmapper:modelmapper:3.2.0'
|
||||
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
implementation 'com.h2database:h2:2.2.224'
|
||||
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
|
||||
|
BIN
demo/data.mv.db
Normal file
BIN
demo/data.mv.db
Normal file
Binary file not shown.
@ -44,15 +44,15 @@ public class DemoApplication implements CommandLineRunner {
|
||||
|
||||
log.info("Create default groups values");
|
||||
|
||||
final var group1 = groupService.create(new GroupEntity(null,user1, "1"));
|
||||
final var group2 = groupService.create(new GroupEntity(null,user2, "2"));
|
||||
final var group3 = groupService.create(new GroupEntity(null,user1, "3"));
|
||||
final var group1 = groupService.create(user1.getId(), new GroupEntity(null,user1, "1"));
|
||||
final var group2 = groupService.create(user2.getId(), new GroupEntity(null,user2, "2"));
|
||||
final var group3 = groupService.create(user1.getId(), new GroupEntity(null,user1, "3"));
|
||||
|
||||
log.info("Create default members values");
|
||||
|
||||
final var member1 = memberService.create(new MemberEntity(null, group1, "mem1", "handle1", "expert"));
|
||||
final var member2 = memberService.create(new MemberEntity(null, group1, "mem2", "handle2", "expert"));
|
||||
final var member3 = memberService.create(new MemberEntity(null, group2,"mem3", "handle3", "expert"));
|
||||
final var member1 = memberService.create(group1.getId(), new MemberEntity(null, group1, "mem1", "handle1", "expert"));
|
||||
final var member2 = memberService.create(group1.getId(), new MemberEntity(null, group1, "mem2", "handle2", "expert"));
|
||||
final var member3 = memberService.create(group2.getId(), new MemberEntity(null, group2,"mem3", "handle3", "expert"));
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,10 @@
|
||||
package com.example.demo.core.configuration;
|
||||
|
||||
public class Constants {
|
||||
public static final String SEQUENCE_NAME = "hibernate_sequence";
|
||||
|
||||
public static final String API_URL = "/api/1.0";
|
||||
|
||||
private Constants() {
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
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));
|
||||
public <T> NotFoundException(Class<T> clazz, Long id) {
|
||||
super(String.format("%s with id [%s] is not found or not exists", clazz.getSimpleName(), id));
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,23 @@
|
||||
package com.example.demo.core.model;
|
||||
|
||||
import com.example.demo.core.configuration.Constants;
|
||||
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
import jakarta.persistence.SequenceGenerator;
|
||||
|
||||
@MappedSuperclass
|
||||
public abstract class BaseEntity {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = Constants.SEQUENCE_NAME)
|
||||
@SequenceGenerator(name = Constants.SEQUENCE_NAME, sequenceName = Constants.SEQUENCE_NAME, allocationSize = 1)
|
||||
protected Long id;
|
||||
|
||||
protected BaseEntity() {
|
||||
}
|
||||
|
||||
protected BaseEntity(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return 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();
|
||||
}
|
||||
}
|
@ -45,27 +45,36 @@ public class GroupController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<GroupDto> getAll(@RequestParam(name = "userId", defaultValue = "0") Long userId) {
|
||||
return groupService.getAll(userId).stream().map(this::toDto).toList();
|
||||
public List<GroupDto> getAll(
|
||||
@PathVariable(name = "user") Long userId) {
|
||||
return groupService.getAll(userId).stream()
|
||||
.map(this::toDto)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public GroupDto get(@PathVariable(name = "id") Long id) {
|
||||
public GroupDto get(
|
||||
@PathVariable(name = "id") Long id) {
|
||||
return toDto(groupService.get(id));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public GroupDto create(@RequestBody @Valid GroupDto dto) {
|
||||
return toDto(groupService.create(toEntity(dto)));
|
||||
public GroupDto create(
|
||||
@PathVariable(name = "user") Long userId,
|
||||
@RequestBody @Valid GroupDto dto) {
|
||||
return toDto(groupService.create(userId, toEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public GroupDto update(@PathVariable(name = "id") Long id, @RequestBody GroupDto dto) {
|
||||
public GroupDto update(
|
||||
@PathVariable(name = "id") Long id,
|
||||
@RequestBody @Valid GroupDto dto) {
|
||||
return toDto(groupService.update(id, toEntity(dto)));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public GroupDto delete(@PathVariable(name = "id") @Valid Long id) {
|
||||
public GroupDto delete(
|
||||
@PathVariable(name = "id") Long id) {
|
||||
return toDto(groupService.delete(id));
|
||||
}
|
||||
}
|
||||
|
@ -3,17 +3,18 @@ package com.example.demo.groups.api;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class GroupDto {
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
private Long id;
|
||||
@NotNull
|
||||
@Min(1)
|
||||
private Long userId;
|
||||
@NotNull
|
||||
@NotBlank
|
||||
private String name;
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
@ -1,20 +1,38 @@
|
||||
package com.example.demo.groups.model;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
import com.example.demo.members.model.MemberEntity;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.OrderBy;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "groups")
|
||||
public class GroupEntity extends BaseEntity {
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "typeId", nullable = false)
|
||||
private UserEntity user;
|
||||
@Column(nullable = false, length = 30)
|
||||
private String name;
|
||||
@OneToMany(mappedBy = "group", cascade = CascadeType.ALL)
|
||||
@OrderBy("id ASC")
|
||||
private Set<MemberEntity> members = new HashSet<>();
|
||||
|
||||
public GroupEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public GroupEntity(Long id, UserEntity user, String name) {
|
||||
super(id);
|
||||
this.user = user;
|
||||
this.name = name;
|
||||
}
|
||||
@ -35,6 +53,24 @@ public class GroupEntity extends BaseEntity {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Set<MemberEntity> getMembers() {
|
||||
return members;
|
||||
}
|
||||
|
||||
public void addMember(MemberEntity member) {
|
||||
if (member.getGroup() != this) {
|
||||
member.setGroup(null);
|
||||
}
|
||||
members.add(member);
|
||||
}
|
||||
|
||||
public void delterMember(MemberEntity member) {
|
||||
if (member.getGroup() != this) {
|
||||
return;
|
||||
}
|
||||
members.remove(member);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, name, user);
|
||||
@ -49,6 +85,7 @@ public class GroupEntity extends BaseEntity {
|
||||
final GroupEntity other = (GroupEntity) obj;
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getName(), name)
|
||||
&& Objects.equals(other.getUser(), user);
|
||||
&& Objects.equals(other.getUser(), user)
|
||||
&& Objects.equals(other.getMembers(), members);
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,16 @@
|
||||
package com.example.demo.groups.repository;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.example.demo.core.repository.MapRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.example.demo.groups.model.GroupEntity;
|
||||
import com.example.demo.groups.model.GroupEntity;
|
||||
|
||||
@Repository
|
||||
public class GroupRepository extends MapRepository<GroupEntity> {
|
||||
public interface GroupRepository extends CrudRepository<GroupEntity, Long> {
|
||||
Optional<GroupEntity> findOneById(long id);
|
||||
|
||||
List<GroupEntity> findByUserId(long userId);
|
||||
}
|
@ -1,55 +1,69 @@
|
||||
package com.example.demo.groups.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.swing.GroupLayout.Group;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.groups.model.GroupEntity;
|
||||
import com.example.demo.groups.repository.GroupRepository;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
import com.example.demo.users.repository.UserRepository;
|
||||
import com.example.demo.users.service.UserService;
|
||||
|
||||
@Service
|
||||
public class GroupService {
|
||||
private final GroupRepository repository;
|
||||
private final UserService userService;
|
||||
|
||||
public GroupService(GroupRepository repository) {
|
||||
public GroupService(GroupRepository repository, UserService userService) {
|
||||
this.repository = repository;
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<GroupEntity> getAll(Long userId) {
|
||||
if (Objects.equals(userId, 0L)) {
|
||||
return repository.getAll();
|
||||
}
|
||||
return repository.getAll().stream()
|
||||
.filter(item -> item.getUser().getId().equals(userId))
|
||||
.toList();
|
||||
userService.get(userId);
|
||||
return repository.findByUserId(userId);
|
||||
}
|
||||
|
||||
public GroupEntity get(Long id) {
|
||||
return Optional.ofNullable(repository.get(id))
|
||||
.orElseThrow(() -> new NotFoundException(id));
|
||||
@Transactional(readOnly = true)
|
||||
public GroupEntity get(long id) {
|
||||
return repository.findById(id)
|
||||
.orElseThrow(() -> new NotFoundException(GroupEntity.class, id));
|
||||
}
|
||||
|
||||
public GroupEntity create(GroupEntity entity) {
|
||||
return repository.create(entity);
|
||||
@Transactional
|
||||
public GroupEntity create(long userId, GroupEntity entity) {
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException("Entity is null");
|
||||
}
|
||||
final UserEntity existsUser = userService.get(userId);
|
||||
entity.setUser(existsUser);
|
||||
return repository.save(entity);
|
||||
}
|
||||
|
||||
public GroupEntity update(Long id, GroupEntity entity) {
|
||||
|
||||
@Transactional
|
||||
public GroupEntity update(long id, GroupEntity entity) {
|
||||
final GroupEntity existsEntity = get(id);
|
||||
existsEntity.setName(entity.getName());
|
||||
existsEntity.setUser(entity.getUser());
|
||||
return repository.update(existsEntity);
|
||||
return repository.save(existsEntity);
|
||||
}
|
||||
|
||||
public GroupEntity delete(Long id) {
|
||||
@Transactional
|
||||
public GroupEntity delete(long id) {
|
||||
final GroupEntity existsEntity = get(id);
|
||||
return repository.delete(existsEntity);
|
||||
repository.delete(existsEntity);
|
||||
return existsEntity;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void clear(){
|
||||
repository.deleteAll();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ public class MemberController {
|
||||
|
||||
@PostMapping
|
||||
public MemberDto create(@RequestBody @Valid MemberDto dto) {
|
||||
return toDto(memberService.create(toEntity(dto)));
|
||||
return toDto(memberService.create(dto.getGroupId(), toEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
|
@ -3,21 +3,22 @@ package com.example.demo.members.api;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class MemberDto {
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
private Long id;
|
||||
@NotNull
|
||||
@Min(1)
|
||||
private Long groupId;
|
||||
@NotNull
|
||||
@NotBlank
|
||||
private String name;
|
||||
@NotNull
|
||||
@NotBlank
|
||||
private String handle;
|
||||
@NotNull
|
||||
@NotBlank
|
||||
private String rank;
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
@ -4,17 +4,31 @@ import java.util.Objects;
|
||||
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
import com.example.demo.groups.model.GroupEntity;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "members")
|
||||
public class MemberEntity extends BaseEntity {
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "typeId", nullable = false)
|
||||
private GroupEntity group;
|
||||
private String name, handle, rank;
|
||||
@Column(nullable = false, length = 50)
|
||||
private String name;
|
||||
@Column(nullable = false, length = 35)
|
||||
private String handle;
|
||||
@Column(nullable = false, length = 30)
|
||||
private String rank;
|
||||
|
||||
public MemberEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MemberEntity(Long id, GroupEntity group, String name, String handle, String rank) {
|
||||
super(id);
|
||||
this.group = group;
|
||||
this.name = name;
|
||||
this.handle = handle;
|
||||
|
@ -2,9 +2,17 @@ package com.example.demo.members.repository;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.example.demo.core.repository.MapRepository;
|
||||
import com.example.demo.members.model.MemberEntity;
|
||||
|
||||
@Repository
|
||||
public class MemberRepository extends MapRepository<MemberEntity> {
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface MemberRepository extends CrudRepository<MemberEntity, Long> {
|
||||
Optional<MemberEntity> findOneById(long id);
|
||||
|
||||
List<MemberEntity> findByGroupId(long groupId);
|
||||
|
||||
}
|
||||
|
@ -1,55 +1,66 @@
|
||||
package com.example.demo.members.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.groups.model.GroupEntity;
|
||||
import com.example.demo.groups.service.GroupService;
|
||||
import com.example.demo.members.model.MemberEntity;
|
||||
import com.example.demo.members.repository.MemberRepository;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
import com.example.demo.users.service.UserService;
|
||||
|
||||
@Service
|
||||
public class MemberService {
|
||||
private final MemberRepository repository;
|
||||
private final GroupService groupService;
|
||||
|
||||
public MemberService(MemberRepository repository) {
|
||||
public MemberService(MemberRepository repository, GroupService groupService) {
|
||||
this.repository = repository;
|
||||
this.groupService = groupService;
|
||||
}
|
||||
|
||||
public List<MemberEntity> getAll(Long groupId) {
|
||||
if (Objects.equals(groupId, 0L)) {
|
||||
return repository.getAll();
|
||||
}
|
||||
return repository.getAll().stream()
|
||||
.filter(item -> item.getGroup().getId().equals(groupId))
|
||||
.toList();
|
||||
@Transactional(readOnly = true)
|
||||
public List<MemberEntity> getAll(long groupId) {
|
||||
return repository.findByGroupId(groupId);
|
||||
}
|
||||
|
||||
public MemberEntity get(Long id) {
|
||||
return Optional.ofNullable(repository.get(id))
|
||||
.orElseThrow(() -> new NotFoundException(id));
|
||||
@Transactional(readOnly = true)
|
||||
public MemberEntity get(long id) {
|
||||
return repository.findOneById(id)
|
||||
.orElseThrow(() -> new NotFoundException(MemberEntity.class, id));
|
||||
}
|
||||
@Transactional
|
||||
public MemberEntity create(long groupId, MemberEntity entity) {
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException("Entity is null");
|
||||
}
|
||||
final GroupEntity existsGroup = groupService.get(groupId);
|
||||
entity.setGroup(existsGroup);
|
||||
return repository.save(entity);
|
||||
}
|
||||
|
||||
public MemberEntity create(MemberEntity entity) {
|
||||
return repository.create(entity);
|
||||
}
|
||||
|
||||
public MemberEntity update(Long id, MemberEntity entity) {
|
||||
@Transactional
|
||||
public MemberEntity update(long id, MemberEntity entity) {
|
||||
final MemberEntity existsEntity = get(id);
|
||||
existsEntity.setGroup(entity.getGroup());
|
||||
existsEntity.setName(entity.getName());
|
||||
existsEntity.setHandle(entity.getHandle());
|
||||
existsEntity.setName(entity.getName());
|
||||
existsEntity.setRank(entity.getRank());
|
||||
return repository.update(existsEntity);
|
||||
|
||||
return repository.save(existsEntity);
|
||||
}
|
||||
|
||||
public MemberEntity delete(Long id) {
|
||||
@Transactional
|
||||
public MemberEntity delete(long id) {
|
||||
final MemberEntity existsEntity = get(id);
|
||||
return repository.delete(existsEntity);
|
||||
repository.delete(existsEntity);
|
||||
return existsEntity;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void clear(){
|
||||
repository.deleteAll();
|
||||
}
|
||||
|
@ -1,20 +1,25 @@
|
||||
package com.example.demo.users.api;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty.Access;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Size;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class UserDto {
|
||||
@JsonProperty(access = Access.READ_ONLY)
|
||||
private Long id;
|
||||
@NotNull
|
||||
@NotBlank
|
||||
@Size(min = 3, max = 25)
|
||||
private String handle;
|
||||
@NotNull
|
||||
@NotBlank
|
||||
@Size(min = 3, max = 30)
|
||||
private String email;
|
||||
@NotNull
|
||||
@NotBlank
|
||||
@Size(min = 3, max = 20)
|
||||
private String password;
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
@ -1,22 +1,38 @@
|
||||
package com.example.demo.users.model;
|
||||
import java.util.Objects;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.swing.GroupLayout.Group;
|
||||
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
import com.example.demo.groups.model.GroupEntity;
|
||||
|
||||
import jakarta.validation.OverridesAttribute;
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.OrderBy;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class UserEntity extends BaseEntity{
|
||||
@Column(nullable = false, unique = true, length = 25)
|
||||
private String handle;
|
||||
@Column(nullable = false, unique = true, length = 30)
|
||||
private String email;
|
||||
@Column(nullable = false, length = 20)
|
||||
private String password;
|
||||
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
|
||||
@OrderBy("id ASC")
|
||||
private Set<GroupEntity> groups = new HashSet<>();
|
||||
|
||||
public UserEntity(){
|
||||
super();
|
||||
}
|
||||
|
||||
public UserEntity(Long id, String handle, String email, String password) {
|
||||
super(id);
|
||||
this.handle = handle;
|
||||
this.email = email;
|
||||
this.password = password;
|
||||
@ -46,6 +62,24 @@ public class UserEntity extends BaseEntity{
|
||||
this.handle = handle;
|
||||
}
|
||||
|
||||
public Set<GroupEntity> getGroups() {
|
||||
return groups;
|
||||
}
|
||||
|
||||
public void addGroup(GroupEntity group) {
|
||||
if (group.getUser() != this) {
|
||||
group.setUser(this);
|
||||
}
|
||||
groups.add(group);
|
||||
}
|
||||
|
||||
public void deleteGroup(GroupEntity group) {
|
||||
if (group.getUser() != this) {
|
||||
return;
|
||||
}
|
||||
groups.remove(group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, handle, email, password);
|
||||
@ -61,7 +95,8 @@ public class UserEntity extends BaseEntity{
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getHandle(), handle)
|
||||
&& Objects.equals(other.getEmail(), email)
|
||||
&& Objects.equals(other.getPassword(), password);
|
||||
&& Objects.equals(other.getPassword(), password)
|
||||
&& Objects.equals(other.getGroups(), groups);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,10 +1,12 @@
|
||||
package com.example.demo.users.repository;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.example.demo.core.repository.MapRepository;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
|
||||
@Repository
|
||||
public class UserRepository extends MapRepository<UserEntity> {
|
||||
public interface UserRepository extends CrudRepository<UserEntity, Long> {
|
||||
Optional<UserEntity> findByHandleIgnoreCase(String login);
|
||||
Optional<UserEntity> findByEmailIgnoreCase(String login);
|
||||
}
|
||||
|
@ -2,9 +2,10 @@ package com.example.demo.users.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
@ -18,32 +19,62 @@ public class UserService {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
private void checkHandle(String handle) {
|
||||
if (repository.findByHandleIgnoreCase(handle).isPresent()) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("User with handle %s is already exists", handle));
|
||||
}
|
||||
}
|
||||
|
||||
private void checkEmail(String email) {
|
||||
if (repository.findByHandleIgnoreCase(email).isPresent()) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("User with email %s is already exists", email));
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<UserEntity> getAll() {
|
||||
return repository.getAll();
|
||||
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
|
||||
}
|
||||
|
||||
public UserEntity get(Long id) {
|
||||
return Optional.ofNullable(repository.get(id))
|
||||
.orElseThrow(() -> new NotFoundException(id));
|
||||
@Transactional(readOnly = true)
|
||||
public UserEntity get(long id) {
|
||||
return repository.findById(id)
|
||||
.orElseThrow(() -> new NotFoundException(UserEntity.class, id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UserEntity create(UserEntity entity) {
|
||||
return repository.create(entity);
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException("Entity is null");
|
||||
}
|
||||
checkHandle(entity.getHandle());
|
||||
checkEmail(entity.getEmail());
|
||||
repository.save(entity);
|
||||
return repository.save(entity);
|
||||
}
|
||||
|
||||
public UserEntity update(Long id, UserEntity entity) {
|
||||
@Transactional
|
||||
public UserEntity update(long id, UserEntity entity) {
|
||||
final UserEntity existsEntity = get(id);
|
||||
existsEntity.setEmail(entity.getEmail());
|
||||
checkHandle(entity.getHandle());
|
||||
checkEmail(entity.getEmail());
|
||||
existsEntity.setHandle(entity.getHandle());
|
||||
existsEntity.setEmail(entity.getEmail());
|
||||
existsEntity.setPassword(entity.getPassword());
|
||||
return repository.update(existsEntity);
|
||||
repository.save(existsEntity);
|
||||
return existsEntity;
|
||||
}
|
||||
|
||||
public UserEntity delete(Long id) {
|
||||
@Transactional
|
||||
public UserEntity delete(long id) {
|
||||
final UserEntity existsEntity = get(id);
|
||||
return repository.delete(existsEntity);
|
||||
repository.delete(existsEntity);
|
||||
return existsEntity;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void clear(){
|
||||
repository.deleteAll();
|
||||
}
|
||||
|
@ -1 +1,19 @@
|
||||
spring.main.banner-mode=off
|
||||
server.port=8080
|
||||
|
||||
# Logger settings
|
||||
# Available levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
|
||||
logging.level.com.example.demo=DEBUG
|
||||
|
||||
# JPA Settings
|
||||
spring.datasource.url=jdbc:h2:file:./data
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=password
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.jpa.hibernate.ddl-auto=create
|
||||
spring.jpa.open-in-view=false
|
||||
# spring.jpa.show-sql=true
|
||||
# spring.jpa.properties.hibernate.format_sql=true
|
||||
|
||||
# H2 console
|
||||
spring.h2.console.enabled=true
|
||||
|
@ -32,8 +32,8 @@ class GroupServiceTests {
|
||||
@Order(1)
|
||||
void createTest() {
|
||||
var user = userService.create(new UserEntity(null, "Oleja123", "bebrus@mail.ru", "1234"));
|
||||
groupService.create(new GroupEntity(null, user, "1"));
|
||||
final GroupEntity last = groupService.create(new GroupEntity(null, user, "2"));
|
||||
groupService.create(user.getId(), new GroupEntity(null, user, "1"));
|
||||
final GroupEntity last = groupService.create(user.getId(), new GroupEntity(null, user, "2"));
|
||||
Assertions.assertEquals(2, groupService.getAll(1L).size());
|
||||
Assertions.assertEquals(last, groupService.get(2L));
|
||||
}
|
||||
@ -59,8 +59,8 @@ class GroupServiceTests {
|
||||
Assertions.assertEquals(1, groupService.getAll(1L).size());
|
||||
final GroupEntity last = groupService.get(1L);
|
||||
Assertions.assertEquals(1L, last.getId());
|
||||
|
||||
final GroupEntity newEntity = groupService.create(new GroupEntity(null, userService.get(1L), test));
|
||||
final UserEntity user = userService.get(1L);
|
||||
final GroupEntity newEntity = groupService.create(user.getId(), new GroupEntity(null, user, test));
|
||||
Assertions.assertEquals(2, groupService.getAll(1L).size());
|
||||
Assertions.assertEquals(3, newEntity.getId());
|
||||
userService.clear();
|
||||
|
@ -37,9 +37,9 @@ class MemberServiceTests {
|
||||
@Order(1)
|
||||
void createTest() {
|
||||
var user = userService.create(new UserEntity(null, "Oleja123", "bebrus@mail.ru", "1234"));
|
||||
var group = groupService.create(new GroupEntity(null, user, "1"));
|
||||
memberService.create(new MemberEntity(null, group, "1", "1", "1"));
|
||||
var last = memberService.create(new MemberEntity(null, group, "2", "2", "2"));
|
||||
var group = groupService.create(user.getId(), new GroupEntity(null, user, "1"));
|
||||
memberService.create(group.getId(), new MemberEntity(null, group, "1", "1", "1"));
|
||||
var last = memberService.create(group.getId(), new MemberEntity(null, group, "2", "2", "2"));
|
||||
Assertions.assertEquals(2, memberService.getAll(1L).size());
|
||||
Assertions.assertEquals(last, memberService.get(2L));
|
||||
}
|
||||
@ -68,8 +68,8 @@ class MemberServiceTests {
|
||||
Assertions.assertEquals(1, memberService.getAll(1L).size());
|
||||
final MemberEntity last = memberService.get(1L);
|
||||
Assertions.assertEquals(1L, last.getId());
|
||||
|
||||
final MemberEntity newEntity = memberService.create(new MemberEntity(null, groupService.get(1L), test,test,test));
|
||||
final GroupEntity group = groupService.get(1L);
|
||||
final MemberEntity newEntity = memberService.create(group.getId(), new MemberEntity(null, group, test,test,test));
|
||||
Assertions.assertEquals(2, memberService.getAll(1L).size());
|
||||
Assertions.assertEquals(3, newEntity.getId());
|
||||
userService.clear();
|
||||
|
Loading…
Reference in New Issue
Block a user