diff --git a/src/main/java/com/totopia/server/modules/game/controller/LeagueController.java b/src/main/java/com/totopia/server/modules/game/controller/LeagueController.java new file mode 100644 index 0000000..5b9093a --- /dev/null +++ b/src/main/java/com/totopia/server/modules/game/controller/LeagueController.java @@ -0,0 +1,56 @@ +package com.totopia.server.modules.game.controller; + +import com.totopia.server.commons.data.payload.ApiResponse; +import com.totopia.server.modules.game.entity.LeagueEntity; +import com.totopia.server.modules.game.service.LeagueService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.web.SortDefault; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; + +@RestController +public class LeagueController { + + @Autowired + private LeagueService leagueService; + + @GetMapping(value = "/league") + public Page all( + @SortDefault.SortDefaults({ + @SortDefault( + sort = "createdAt", + direction = Sort.Direction.DESC) }) + Pageable pageable) { + return this.leagueService.getAllLeagueByPageable(pageable); + } + + @GetMapping(value = "/league/{sprotName}") + public ResponseEntity findBySportsName(@PathVariable String sportName) { + return null; + } + + @PostMapping(value = "/league") + @ResponseStatus(code = HttpStatus.CREATED) + public ResponseEntity save(@Valid @RequestBody LeagueEntity leagueEntity) throws Exception{ + this.leagueService.save(leagueEntity); + return ResponseEntity.ok().body(new ApiResponse(true, "League registered successfully")); + } + + @PutMapping(value = "/league/{leagueId}") + public ResponseEntity updateLeague(@PathVariable Long leagueId, LeagueEntity leagueEntity) throws Exception { + LeagueEntity leagueEntity1 = this.leagueService.modify(leagueEntity); + return ResponseEntity.ok(leagueEntity1); + } + + @DeleteMapping(value = "/league/{leagueId}") + public ResponseEntity deleteLeague(@PathVariable Long leagueId) { + Long res = this.leagueService.removeById(leagueId); + return ResponseEntity.ok(res); + } +} diff --git a/src/main/java/com/totopia/server/modules/game/service/LeagueService.java b/src/main/java/com/totopia/server/modules/game/service/LeagueService.java new file mode 100644 index 0000000..539581c --- /dev/null +++ b/src/main/java/com/totopia/server/modules/game/service/LeagueService.java @@ -0,0 +1,45 @@ +package com.totopia.server.modules.game.service; + +import com.totopia.server.commons.exception.ResourceNotFoundException; +import com.totopia.server.modules.game.entity.LeagueEntity; +import com.totopia.server.modules.game.repository.LeagueRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +@Service +public class LeagueService { + + @Autowired + private LeagueRepository leagueRepository; + + public LeagueEntity save(LeagueEntity leagueEntity) { + return this.leagueRepository.save(leagueEntity); + } + + public LeagueEntity modify(LeagueEntity leagueEntity) { + return this.leagueRepository.save(leagueEntity); + } + + public Page getAllLeagueByPageable(Pageable pageable) { + Page entities = null; + +// if (null == username) { + entities = leagueRepository.findAll(pageable); +// } else { +// entities = userRepository.findByUsernameContainingIgnoreCase(username, pageable); +// } + + return entities; + } + + public Long removeById(Long leagueId) { + return leagueRepository.findById(leagueId).map(league -> { + leagueRepository.delete(league); + return leagueId; + }).orElseThrow(() -> new ResourceNotFoundException("League", "leagueId", leagueId)); + } + + public void getLeagueById() {} +} diff --git a/src/main/java/com/totopia/server/modules/game/service/SportsService.java b/src/main/java/com/totopia/server/modules/game/service/SportsService.java new file mode 100644 index 0000000..1d2df42 --- /dev/null +++ b/src/main/java/com/totopia/server/modules/game/service/SportsService.java @@ -0,0 +1,41 @@ +package com.totopia.server.modules.game.service; + +import com.totopia.server.commons.exception.ResourceNotFoundException; +import com.totopia.server.modules.game.entity.SportsEntity; +import com.totopia.server.modules.game.repository.SportsRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +@Service +public class SportsService { + + @Autowired + private SportsRepository sportsRepository; + + public Page getAllSportsByPageable(Pageable pageable) { + Page entities = null; + entities = this.sportsRepository.findAll(pageable); + return entities; + } + + public SportsEntity getSportsById(Short sportId) { + return this.sportsRepository.findById(sportId).orElseThrow(null); + } + + public SportsEntity save(SportsEntity sportsEntity) { + return this.sportsRepository.save(sportsEntity); + } + + public SportsEntity modify(SportsEntity sportsEntity) { + return this.sportsRepository.save(sportsEntity); + } + + public Short remove(Short sportsId) { + return sportsRepository.findById(sportsId).map(sports -> { + sportsRepository.delete(sports); + return sportsId; + }).orElseThrow(() -> new ResourceNotFoundException("Sports", "sportsId", sportsId)); + } +} diff --git a/src/main/java/com/totopia/server/modules/user/controller/UserController.java b/src/main/java/com/totopia/server/modules/user/controller/UserController.java index acc9a95..40d9878 100644 --- a/src/main/java/com/totopia/server/modules/user/controller/UserController.java +++ b/src/main/java/com/totopia/server/modules/user/controller/UserController.java @@ -2,10 +2,8 @@ package com.totopia.server.modules.user.controller; import com.totopia.server.auth.payload.SignupRequest; import com.totopia.server.commons.data.payload.ApiResponse; -import com.totopia.server.commons.exception.ResourceNotFoundException; import com.totopia.server.modules.user.entity.UserEntity; import com.totopia.server.modules.user.payload.UserUpdateRequest; -import com.totopia.server.modules.user.repository.UserRepository; import com.totopia.server.modules.user.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.*; @@ -13,26 +11,22 @@ import org.springframework.data.web.SortDefault; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import org.springframework.web.servlet.support.ServletUriComponentsBuilder; import javax.validation.Valid; -import java.net.URI; @RestController public class UserController { - @Autowired - private UserRepository userRepository; @Autowired private UserService userService; - @PostMapping(value = "/regist") + @PostMapping(value = "/users") @ResponseStatus(code = HttpStatus.CREATED) public ResponseEntity save(@Valid @RequestBody SignupRequest signupRequest) throws Exception{ // User user 파라미터는 추후에 수 if (this.userService.existUserByUsername(signupRequest.getUsername())) { return new ResponseEntity( - ApiResponse.builder().success(false).message("Username is already taken!").build(), HttpStatus.BAD_REQUEST); + ApiResponse.builder().success(false).message("Username is already taken!").build(), HttpStatus.BAD_REQUEST); } this.userService.regist(signupRequest); @@ -43,17 +37,17 @@ public class UserController { @GetMapping(value = "/users") public Page all(@RequestParam(value = "username", required = false) String username, @SortDefault.SortDefaults({ - @SortDefault( - sort = "createdAt", - direction = Sort.Direction.DESC) }) - Pageable pageable) { + @SortDefault( + sort = "createdAt", + direction = Sort.Direction.DESC) }) + Pageable pageable) { - return this.userService.getUsersByPageable(username, pageable); + return this.userService.getAllUsersByPageable(username, pageable); } - @GetMapping(value = "/connect-users") + @GetMapping(value = "/users/connect-users") public Page connectUsers(@RequestParam(value = "username", required = false) String username, - @SortDefault.SortDefaults({ @SortDefault(sort = "createdAt", direction = Sort.Direction.DESC) }) Pageable pageable) { + @SortDefault.SortDefaults({ @SortDefault(sort = "createdAt", direction = Sort.Direction.DESC) }) Pageable pageable) { // final List curUsers = sessionRegistry.getAllPrincipals().stream() // .filter(u -> !sessionRegistry.getAllSessions(u, false).isEmpty()) // .map(Object::toString) @@ -73,7 +67,7 @@ public class UserController { // return new PageImpl(users, new PageRequest(pageable.getPageNumber(), pageable.getPageSize()), users.size()); return null; } - @GetMapping(value = "/users/{userId}") + @GetMapping(value = "/users/{userId}") public UserEntity findByUserId(@PathVariable Long userId) { return this.userService.getUserById(userId); } @@ -92,7 +86,7 @@ public class UserController { if (existrEntity == null) { return new ResponseEntity( - ApiResponse.builder().success(false).message("User ID does not exist").build(), HttpStatus.BAD_REQUEST); + ApiResponse.builder().success(false).message("User ID does not exist").build(), HttpStatus.BAD_REQUEST); } UserEntity userEntity = this.userService.modifyByUser(existrEntity, updateRequest); diff --git a/src/main/java/com/totopia/server/modules/user/entity/UserEntity.java b/src/main/java/com/totopia/server/modules/user/entity/UserEntity.java index 8427c37..dfba7f7 100644 --- a/src/main/java/com/totopia/server/modules/user/entity/UserEntity.java +++ b/src/main/java/com/totopia/server/modules/user/entity/UserEntity.java @@ -2,10 +2,7 @@ package com.totopia.server.modules.user.entity; import com.fasterxml.jackson.annotation.JsonIgnore; import com.totopia.server.commons.data.entity.DateAuditEntity; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.NoArgsConstructor; +import lombok.*; import lombok.experimental.SuperBuilder; import javax.persistence.*; @@ -19,6 +16,7 @@ import java.util.Set; @NoArgsConstructor @AllArgsConstructor @EqualsAndHashCode(callSuper = false) +@Builder public class UserEntity extends DateAuditEntity { private static final long serialVersionUID = 8891163223262220481L; diff --git a/src/main/java/com/totopia/server/modules/user/service/UserService.java b/src/main/java/com/totopia/server/modules/user/service/UserService.java index 14cf1f0..7d46b9f 100644 --- a/src/main/java/com/totopia/server/modules/user/service/UserService.java +++ b/src/main/java/com/totopia/server/modules/user/service/UserService.java @@ -29,7 +29,7 @@ public class UserService { @Autowired PasswordEncoder passwordEncoder; - public Page getUsersByPageable(String username, Pageable pageable) { + public Page getAllUsersByPageable(String username, Pageable pageable) { Page entities = null; if (null == username) { @@ -47,7 +47,7 @@ public class UserService { public UserEntity getUserById(Long id) { UserEntity userEntity = this.userRepository.findById(id) - .orElseThrow( () -> new ResourceNotFoundException("User", "userId", id)); + .orElseThrow( () -> new ResourceNotFoundException("User", "userId", id)); return userEntity; } @@ -61,16 +61,15 @@ public class UserService { public void regist(SignupRequest signupRequest) throws Exception{ - // Creating user's account UserEntity user = UserEntity.builder().username(signupRequest.getUsername()).email(signupRequest.getEmail()) - .nickname(signupRequest.getNickname()).phone(signupRequest.getPhone()).descriptions(signupRequest.getDescriptions()) - .password(signupRequest.getPassword()).block(false).resetCount(0L).sendEmail(true).build(); + .nickname(signupRequest.getNickname()).phone(signupRequest.getPhone()).descriptions(signupRequest.getDescriptions()) + .password(signupRequest.getPassword()).block(false).resetCount(0L).sendEmail(true).build(); user.setPassword(passwordEncoder.encode(user.getPassword())); RoleEntity userRole = roleRepository.findByName(RoleName.ROLE_USER) - .orElseThrow(() -> new Exception("User Role not set.")); + .orElseThrow(() -> new Exception("User Role not set.")); user.setRoles(Collections.singleton(userRole)); @@ -92,10 +91,10 @@ public class UserService { String newDescriptions = updateRequest.getDescriptions(); if (newNickname.equals(existNickname) - && newPhone.equals(existPhone) - && newEmail.equals(existEmail) - && this.passwordEncoder.matches(newPassword,existPassword) - && newDescriptions.equals(existDescriptions) + && newPhone.equals(existPhone) + && newEmail.equals(existEmail) + && this.passwordEncoder.matches(newPassword,existPassword) + && newDescriptions.equals(existDescriptions) ) { return existEntity; } diff --git a/src/test/java/com/totopia/server/modules/game/service/LeagueServiceTest.java b/src/test/java/com/totopia/server/modules/game/service/LeagueServiceTest.java new file mode 100644 index 0000000..a4f90a4 --- /dev/null +++ b/src/test/java/com/totopia/server/modules/game/service/LeagueServiceTest.java @@ -0,0 +1,49 @@ +package com.totopia.server.modules.game.service; + +import com.totopia.server.modules.game.entity.LeagueEntity; +import com.totopia.server.modules.game.repository.LeagueRepository; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.*; + +@Ignore +@RunWith(SpringRunner.class) +@SpringBootTest +public class LeagueServiceTest { + + @Autowired + LeagueRepository leagueRepository; + + @Test + public void save() { + } + + @Test + public void modify() { + } + + @Test + public void getAllLeagueByPageable() { +// new PageImpl(users, new PageRequest(pageable.getPageNumber(), pageable.getPageSize()), users.size()); + Pageable pageable = PageRequest.of(0, 10); + Page entities = this.leagueRepository.findAll(pageable); + + assertEquals(10, entities.getContent().size()); + } + + @Test + public void removeById() { + } + + @Test + public void getLeagueById() { + } +} \ No newline at end of file diff --git a/src/test/java/com/totopia/server/modules/game/service/SportsServiceTest.java b/src/test/java/com/totopia/server/modules/game/service/SportsServiceTest.java new file mode 100644 index 0000000..5ff11b3 --- /dev/null +++ b/src/test/java/com/totopia/server/modules/game/service/SportsServiceTest.java @@ -0,0 +1,35 @@ +package com.totopia.server.modules.game.service; + +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.*; + +@Ignore +@RunWith(SpringRunner.class) +@SpringBootTest +public class SportsServiceTest { + + @Test + public void getAllSportsByPageable() { + } + + @Test + public void getSportsById() { + } + + @Test + public void save() { + } + + @Test + public void modify() { + } + + @Test + public void remove() { + } +} \ No newline at end of file