리그 서비스 클래스 추

This commit is contained in:
byung eun park 2019-09-02 18:34:48 +09:00
parent 78037ab0d4
commit a6cfca98aa
8 changed files with 248 additions and 31 deletions

View File

@ -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<LeagueEntity> all(
@SortDefault.SortDefaults({
@SortDefault(
sort = "createdAt",
direction = Sort.Direction.DESC) })
Pageable pageable) {
return this.leagueService.getAllLeagueByPageable(pageable);
}
@GetMapping(value = "/league/{sprotName}")
public ResponseEntity<LeagueEntity> 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);
}
}

View File

@ -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<LeagueEntity> getAllLeagueByPageable(Pageable pageable) {
Page<LeagueEntity> 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() {}
}

View File

@ -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<SportsEntity> getAllSportsByPageable(Pageable pageable) {
Page<SportsEntity> 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));
}
}

View File

@ -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>(
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<UserEntity> 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<UserEntity> 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<String> curUsers = sessionRegistry.getAllPrincipals().stream()
// .filter(u -> !sessionRegistry.getAllSessions(u, false).isEmpty())
// .map(Object::toString)
@ -73,7 +67,7 @@ public class UserController {
// return new PageImpl<UserEntity>(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>(
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);

View File

@ -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;

View File

@ -29,7 +29,7 @@ public class UserService {
@Autowired
PasswordEncoder passwordEncoder;
public Page<UserEntity> getUsersByPageable(String username, Pageable pageable) {
public Page<UserEntity> getAllUsersByPageable(String username, Pageable pageable) {
Page<UserEntity> 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;
}

View File

@ -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<UserEntity>(users, new PageRequest(pageable.getPageNumber(), pageable.getPageSize()), users.size());
Pageable pageable = PageRequest.of(0, 10);
Page<LeagueEntity> entities = this.leagueRepository.findAll(pageable);
assertEquals(10, entities.getContent().size());
}
@Test
public void removeById() {
}
@Test
public void getLeagueById() {
}
}

View File

@ -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() {
}
}