-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPetController.java
More file actions
76 lines (62 loc) ยท 2.75 KB
/
PetController.java
File metadata and controls
76 lines (62 loc) ยท 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package cmf.commitField.domain.pet.controller;
import cmf.commitField.domain.pet.dto.PetsDto;
import cmf.commitField.domain.pet.dto.UserPetDto;
import cmf.commitField.domain.pet.entity.Pet;
import cmf.commitField.domain.pet.service.PetService;
import cmf.commitField.domain.user.entity.CustomOAuth2User;
import cmf.commitField.domain.user.entity.User;
import cmf.commitField.domain.user.service.CustomOAuth2UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/pets")
public class PetController {
private final CustomOAuth2UserService userService;
private final PetService petService;
// ํ์ฌ ํซ ๊ฒฝํ์น ์์น ๋ฐ ์์น ์ ๋ ๋ฒจ์
์ฒ๋ฆฌ
@GetMapping("/getall")
public ResponseEntity<List<PetsDto>> getUserPets(@AuthenticationPrincipal CustomOAuth2User oAuth2User){
String username = oAuth2User.getName(); // CustomOAuth2User์ getName()์ user.getUsername()์ ๋ฐํ
List<PetsDto> userPetDto = petService.getAllPets(username);
return ResponseEntity.ok(userPetDto);
}
// ํ์ฌ ํซ ๊ฒฝํ์น ์์น ๋ฐ ์์น ์ ๋ ๋ฒจ์
์ฒ๋ฆฌ
@GetMapping("/exp")
public ResponseEntity<UserPetDto> getPetExp(@AuthenticationPrincipal CustomOAuth2User oAuth2User){
String username = oAuth2User.getName();
System.out.println("/pet/exp, Username: "+username);
UserPetDto userPetDto = petService.getExpPet(username, 0);
return ResponseEntity.ok(userPetDto);
}
// *************************************
// TODO : ์๋๋ ํ์ฅ ๊ธฐ๋ฅ, ์ถ๊ฐ ์ ๊ฐ์ ํ์
// *************************************
// ์๋ก์ด ํซ ์ถ๊ฐ
@PostMapping("/new")
public ResponseEntity<Pet> createPet(@AuthenticationPrincipal CustomOAuth2User oAuth2User) throws Exception {
String username = oAuth2User.getName();
Pet pet = petService.createPet("์์", username);
if(pet == null){
//์ฌ์ฉ์๊ฐ ํ์ฌ ํซ์ด GROWN ์ํ๊ฐ ์๋๋ฐ๋ ๋ค๋ฅธ ๊ฒฝ๋ก๋ฅผ ํตํด ์์ฒญํ ๊ฒฝ์ฐ
ResponseEntity.badRequest();
}
return ResponseEntity.ok(pet);
}
// ํน์ ํซ ์กฐํ
@GetMapping("/{petId}")
public Pet getPetById(@PathVariable Long petId) {
return petService.getPetById(petId);
}
// ํซ ์ญ์
@DeleteMapping("/{petId}")
public void deletePet(@PathVariable Long petId) {
petService.deletePet(petId);
}
public void getExpPet(User user, int commitCount){
petService.getExpPet(user.getUsername(), commitCount);
}
}