СДАЛ СДАЛ СДАЛ
This commit is contained in:
parent
a7366ae96a
commit
56ad0f134a
BIN
demo/data.mv.db
BIN
demo/data.mv.db
Binary file not shown.
1722
demo/data.trace.db
1722
demo/data.trace.db
File diff suppressed because it is too large
Load Diff
@ -51,9 +51,13 @@ public class DemoApplication implements CommandLineRunner {
|
||||
|
||||
log.info("Create default members values");
|
||||
|
||||
final var member1 = memberService.create(group1.getId(), new MemberEntity("mem1", "handle1", "expert"));
|
||||
final var member2 = memberService.create(group1.getId(), new MemberEntity( "mem2", "handle2", "expert"));
|
||||
final var member3 = memberService.create(group2.getId(), new MemberEntity("mem3", "handle3", "expert"));
|
||||
final var member1 = memberService.create(group1.getId(), new MemberEntity("mem1", "handle1", "expert", 1600L));
|
||||
final var member2 = memberService.create(group1.getId(), new MemberEntity( "mem2", "handle2", "expert", 1715L));
|
||||
final var member3 = memberService.create(group1.getId(), new MemberEntity("mem1", "handle1", "expert", 1800L));
|
||||
final var member4 = memberService.create(group1.getId(), new MemberEntity( "mem2", "handle2", "expert", 1815L));
|
||||
final var member5 = memberService.create(group1.getId(), new MemberEntity("mem1", "handle1", "expert", 1611L));
|
||||
final var member6 = memberService.create(group1.getId(), new MemberEntity( "mem2", "handle2", "expert", 1717L));
|
||||
final var member7 = memberService.create(group2.getId(), new MemberEntity("mem3", "handle3", "expert", 1720L));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
97
demo/src/main/java/com/example/demo/core/api/PageDto.java
Normal file
97
demo/src/main/java/com/example/demo/core/api/PageDto.java
Normal file
@ -0,0 +1,97 @@
|
||||
package com.example.demo.core.api;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PageDto<D> {
|
||||
private List<D> items = new ArrayList<>();
|
||||
private int itemsCount;
|
||||
private int currentPage;
|
||||
private int currentSize;
|
||||
private int totalPages;
|
||||
private long totalItems;
|
||||
private boolean isFirst;
|
||||
private boolean isLast;
|
||||
private boolean hasNext;
|
||||
private boolean hasPrevious;
|
||||
|
||||
public List<D> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public void setItems(List<D> items) {
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
public int getItemsCount() {
|
||||
return itemsCount;
|
||||
}
|
||||
|
||||
public void setItemsCount(int itemsCount) {
|
||||
this.itemsCount = itemsCount;
|
||||
}
|
||||
|
||||
public int getCurrentPage() {
|
||||
return currentPage;
|
||||
}
|
||||
|
||||
public void setCurrentPage(int currentPage) {
|
||||
this.currentPage = currentPage;
|
||||
}
|
||||
|
||||
public int getCurrentSize() {
|
||||
return currentSize;
|
||||
}
|
||||
|
||||
public void setCurrentSize(int currentSize) {
|
||||
this.currentSize = currentSize;
|
||||
}
|
||||
|
||||
public int getTotalPages() {
|
||||
return totalPages;
|
||||
}
|
||||
|
||||
public void setTotalPages(int totalPages) {
|
||||
this.totalPages = totalPages;
|
||||
}
|
||||
|
||||
public long getTotalItems() {
|
||||
return totalItems;
|
||||
}
|
||||
|
||||
public void setTotalItems(long totalItems) {
|
||||
this.totalItems = totalItems;
|
||||
}
|
||||
|
||||
public boolean isFirst() {
|
||||
return isFirst;
|
||||
}
|
||||
|
||||
public void setFirst(boolean isFirst) {
|
||||
this.isFirst = isFirst;
|
||||
}
|
||||
|
||||
public boolean isLast() {
|
||||
return isLast;
|
||||
}
|
||||
|
||||
public void setLast(boolean isLast) {
|
||||
this.isLast = isLast;
|
||||
}
|
||||
|
||||
public boolean isHasNext() {
|
||||
return hasNext;
|
||||
}
|
||||
|
||||
public void setHasNext(boolean hasNext) {
|
||||
this.hasNext = hasNext;
|
||||
}
|
||||
|
||||
public boolean isHasPrevious() {
|
||||
return hasPrevious;
|
||||
}
|
||||
|
||||
public void setHasPrevious(boolean hasPrevious) {
|
||||
this.hasPrevious = hasPrevious;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.example.demo.core.api;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
|
||||
public class PageDtoMapper {
|
||||
private PageDtoMapper() {
|
||||
}
|
||||
|
||||
public static <D, E> PageDto<D> toDto(Page<E> page, Function<E, D> mapper) {
|
||||
final PageDto<D> dto = new PageDto<>();
|
||||
dto.setItems(page.getContent().stream().map(mapper::apply).toList());
|
||||
dto.setItemsCount(page.getNumberOfElements());
|
||||
dto.setCurrentPage(page.getNumber());
|
||||
dto.setCurrentSize(page.getSize());
|
||||
dto.setTotalPages(page.getTotalPages());
|
||||
dto.setTotalItems(page.getTotalElements());
|
||||
dto.setFirst(page.isFirst());
|
||||
dto.setLast(page.isLast());
|
||||
dto.setHasNext(page.hasNext());
|
||||
dto.setHasPrevious(page.hasPrevious());
|
||||
return dto;
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ package com.example.demo.members.api;
|
||||
import java.util.List;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
@ -13,6 +14,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.example.demo.core.api.PageDto;
|
||||
import com.example.demo.core.api.PageDtoMapper;
|
||||
import com.example.demo.core.configuration.Constants;
|
||||
import com.example.demo.members.model.MemberEntity;
|
||||
import com.example.demo.members.service.MemberService;
|
||||
@ -37,13 +40,34 @@ public class MemberController {
|
||||
return modelMapper.map(entity, MemberDto.class);
|
||||
}
|
||||
|
||||
|
||||
private MemberEntity toEntity(MemberDto dto) {
|
||||
final MemberEntity entity = modelMapper.map(dto, MemberEntity.class);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@GetMapping("/getmemberstop")
|
||||
public List<MemberDto> getMembersTop() {
|
||||
return memberService.getMembersTop(5).stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/getmemberstoplist")
|
||||
public List<MemberDto> getMembersTopList() {
|
||||
return memberService.getMembersTopList(5).stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/getmemberstopgrouplist")
|
||||
public List<MemberDto> getMembersTopGroupList(@RequestParam(name = "groupId", defaultValue = "0") Long groupId) {
|
||||
return memberService.getMembersTopGroupList(5, groupId).stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/getmemberstopgroup")
|
||||
public List<MemberDto> getMembersTopGroup(@RequestParam(name = "groupId", defaultValue = "0") Long groupId) {
|
||||
return memberService.getMembersTopGroup(5, groupId).stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<MemberDto> getAll(@RequestParam(name = "groupId", defaultValue = "0") Long groupId) {
|
||||
public List<MemberDto> getAll(@RequestParam(name = "group", defaultValue = "0") Long groupId) {
|
||||
return memberService.getAll(groupId).stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
|
@ -15,6 +15,8 @@ public class MemberDto {
|
||||
private String handle;
|
||||
@NotBlank
|
||||
private String rank;
|
||||
@NotBlank
|
||||
private Long rating;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
@ -24,6 +26,14 @@ public class MemberDto {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getRating() {
|
||||
return rating;
|
||||
}
|
||||
|
||||
public void setRating(Long rating) {
|
||||
this.rating = rating;
|
||||
}
|
||||
|
||||
public String getHandle() {
|
||||
return handle;
|
||||
}
|
||||
|
@ -25,14 +25,17 @@ public class MemberEntity extends BaseEntity {
|
||||
private String handle;
|
||||
@Column(nullable = false, length = 30)
|
||||
private String rank;
|
||||
@Column
|
||||
private Long rating;
|
||||
|
||||
public MemberEntity() {
|
||||
}
|
||||
|
||||
public MemberEntity(String name, String handle, String rank) {
|
||||
public MemberEntity(String name, String handle, String rank, Long rating) {
|
||||
this.name = name;
|
||||
this.handle = handle;
|
||||
this.rank = rank;
|
||||
this.rating = rating;
|
||||
}
|
||||
|
||||
public GroupEntity getGroup() {
|
||||
@ -54,6 +57,14 @@ public class MemberEntity extends BaseEntity {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getRating() {
|
||||
return rating;
|
||||
}
|
||||
|
||||
public void setRating(Long rating) {
|
||||
this.rating = rating;
|
||||
}
|
||||
|
||||
public String getHandle() {
|
||||
return handle;
|
||||
}
|
||||
@ -85,6 +96,7 @@ public class MemberEntity extends BaseEntity {
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getName(), name)
|
||||
&& Objects.equals(other.getHandle(), handle)
|
||||
&& Objects.equals(other.getRank(), rank);
|
||||
&& Objects.equals(other.getRank(), rank)
|
||||
&& Objects.equals(other.getRating(), rating);
|
||||
}
|
||||
}
|
@ -7,6 +7,8 @@ import com.example.demo.members.model.MemberEntity;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
@ -14,5 +16,26 @@ public interface MemberRepository extends CrudRepository<MemberEntity, Long> {
|
||||
Optional<MemberEntity> findOneById(long id);
|
||||
|
||||
List<MemberEntity> findByGroupId(long groupId);
|
||||
@Query("select "
|
||||
+ "m"
|
||||
+ " from MemberEntity m"
|
||||
+ " order by m.rating")
|
||||
Page<MemberEntity> getMembersTop(Pageable pageable);
|
||||
|
||||
@Query("select "
|
||||
+ "m"
|
||||
+ " from MemberEntity m where m.group.id = ?1"
|
||||
+ " order by m.rating desc")
|
||||
Page<MemberEntity> getMembersTopGroup(long groupId, Pageable pageable);
|
||||
@Query("select "
|
||||
+ "m"
|
||||
+ " from MemberEntity m"
|
||||
+ " order by m.rating desc")
|
||||
List<MemberEntity> getMembersTopList();
|
||||
|
||||
@Query("select "
|
||||
+ "m"
|
||||
+ " from MemberEntity m where m.group.id = ?1"
|
||||
+ " order by m.rating desc")
|
||||
List<MemberEntity> getMembersTopGroupList(long groupId);
|
||||
}
|
||||
|
@ -2,6 +2,9 @@ package com.example.demo.members.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@ -28,6 +31,35 @@ public class MemberService {
|
||||
return repository.findByGroupId(groupId);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<MemberEntity> getMembersTop(int top) {
|
||||
final Pageable pageRequest = PageRequest.of(0, top);
|
||||
return repository.getMembersTop(pageRequest).getContent();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<MemberEntity> getMembersTopList(int top) {
|
||||
var res = repository.getMembersTopList();
|
||||
if(top > res.size())
|
||||
return res;
|
||||
return res.subList(0, top);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<MemberEntity> getMembersTopGroupList(int top, long groupId) {
|
||||
var res = repository.getMembersTopGroupList(groupId);
|
||||
if(top > res.size())
|
||||
return res;
|
||||
return res.subList(0, top);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<MemberEntity> getMembersTopGroup(int top, long groupId) {
|
||||
final Pageable pageRequest = PageRequest.of(0, top);
|
||||
return repository.getMembersTopGroup(groupId, pageRequest).getContent();
|
||||
}
|
||||
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public MemberEntity get(long id) {
|
||||
return repository.findOneById(id)
|
||||
|
@ -12,8 +12,8 @@ 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
|
||||
spring.jpa.show-sql=true
|
||||
spring.jpa.properties.hibernate.format_sql=true
|
||||
|
||||
# H2 console
|
||||
spring.h2.console.enabled=true
|
||||
|
@ -40,9 +40,9 @@ class MemberServiceTests {
|
||||
|
||||
user = userService.create(new UserEntity("oleg", "tester1", "1234"));
|
||||
group = groupService.create(user.getId(), new GroupEntity("group1"));
|
||||
member = memberService.create(group.getId(), new MemberEntity("member1", "mem1","expert"));
|
||||
memberService.create(group.getId(), new MemberEntity("member1", "mem1","expert"));
|
||||
memberService.create(group.getId(), new MemberEntity("member1", "mem1","expert"));
|
||||
member = memberService.create(group.getId(), new MemberEntity("member1", "mem1","expert", 1615L));
|
||||
memberService.create(group.getId(), new MemberEntity("member1", "mem1","expert", 1712L));
|
||||
memberService.create(group.getId(), new MemberEntity("member1", "mem1","expert", 1651L));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
@ -64,7 +64,7 @@ class MemberServiceTests {
|
||||
|
||||
@Test
|
||||
void createNullableTest() {
|
||||
final MemberEntity nullableMember= new MemberEntity(null, null, null);
|
||||
final MemberEntity nullableMember= new MemberEntity(null, null, null, null);
|
||||
Assertions.assertThrows(DataIntegrityViolationException.class, () -> memberService.create(group.getId(), nullableMember));
|
||||
}
|
||||
|
||||
@ -75,7 +75,7 @@ class MemberServiceTests {
|
||||
final String oldName = member.getName();
|
||||
final String oldHandle = member.getHandle();
|
||||
final String oldRank = member.getRank();
|
||||
final MemberEntity newEntity = memberService.update(member.getId(), new MemberEntity(test, test, test));
|
||||
final MemberEntity newEntity = memberService.update(member.getId(), new MemberEntity(test, test, test, null));
|
||||
Assertions.assertEquals(3, memberService.getAll(group.getId()).size());
|
||||
Assertions.assertEquals(newEntity, memberService.get(member.getId()));
|
||||
Assertions.assertEquals(test, newEntity.getName());
|
||||
@ -91,7 +91,8 @@ class MemberServiceTests {
|
||||
memberService.delete(member.getId());
|
||||
Assertions.assertEquals(2, memberService.getAll(group.getId()).size());
|
||||
|
||||
final MemberEntity newEntity = memberService.create(group.getId(), new MemberEntity(member.getName(), member.getHandle(), member.getRank()));
|
||||
final MemberEntity newEntity = memberService.create(group.getId(), new MemberEntity(member.getName(), member.getHandle(), member.getRank(),
|
||||
member.getRating()));
|
||||
Assertions.assertEquals(3, memberService.getAll(group.getId()).size());
|
||||
Assertions.assertNotEquals(member.getId(), newEntity.getId());
|
||||
}
|
||||
|
@ -48,7 +48,6 @@ class UserServiceTests {
|
||||
@Test
|
||||
void createNotUniqueHandleTest() {
|
||||
final UserEntity nonUniqueHandleUser = new UserEntity("oleg", "tester4", "1234");
|
||||
|
||||
Assertions.assertThrows(IllegalArgumentException.class, () -> userService.create(nonUniqueHandleUser));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user