Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit 1ab0f1a

Browse files
dev/codeforces/ Добавил логику для Team
1 parent 0109a2d commit 1ab0f1a

File tree

7 files changed

+98
-21
lines changed

7 files changed

+98
-21
lines changed

src/main/java/com/cf/cfteam/controllers/codeforces/TeamController.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.cf.cfteam.controllers.codeforces;
22

3-
import com.cf.cfteam.models.entities.codeforces.Team;
43
import com.cf.cfteam.services.codeforces.TeamService;
54
import com.cf.cfteam.transfer.payloads.codeforces.GroupPayload;
65
import com.cf.cfteam.transfer.payloads.codeforces.TeamPayload;
6+
import com.cf.cfteam.transfer.responses.codeforces.TeamResponse;
77
import lombok.RequiredArgsConstructor;
88
import org.springframework.http.ResponseEntity;
99
import org.springframework.security.core.Authentication;
@@ -15,32 +15,32 @@
1515
@RequiredArgsConstructor
1616
@RequestMapping("api/cf/teams")
1717
public class TeamController {
18-
18+
1919
private final TeamService teamService;
20-
20+
2121
@GetMapping("/group/{groupId}")
22-
public ResponseEntity<List<Team>> getAllTeamsByGroup(@PathVariable Long groupId, Authentication authentication) {
23-
List<Team> teams = teamService.getAllTeamsByGroup(groupId);
22+
public ResponseEntity<List<TeamResponse>> getAllTeamsByGroup(@PathVariable Long groupId, Authentication authentication) {
23+
List<TeamResponse> teams = teamService.getAllTeamsByGroup(groupId);
2424
return ResponseEntity.ok(teams);
2525
}
2626

2727
@GetMapping("/{teamId}")
28-
public ResponseEntity<Team> getTeamById(@PathVariable Long teamId, Authentication authentication) {
29-
Team team = teamService.getTeamById(teamId);
28+
public ResponseEntity<TeamResponse> getTeamById(@PathVariable Long teamId, Authentication authentication) {
29+
TeamResponse team = teamService.getTeamById(teamId);
3030
return ResponseEntity.ok(team);
3131
}
3232

3333
@PostMapping("/group/{groupId}")
34-
public ResponseEntity<Team> addTeamToGroup(@PathVariable Long groupId, @RequestBody TeamPayload teamPayload,
34+
public ResponseEntity<TeamResponse> addTeamToGroup(@PathVariable Long groupId, @RequestBody TeamPayload teamPayload,
3535
Authentication authentication) {
36-
Team createdTeam = teamService.addTeamToGroup(groupId, teamPayload);
36+
TeamResponse createdTeam = teamService.addTeamToGroup(groupId, teamPayload);
3737
return ResponseEntity.ok(createdTeam);
3838
}
3939

4040
@PutMapping("/{teamId}")
41-
public ResponseEntity<Team> updateTeam(@PathVariable Long teamId, @RequestBody GroupPayload groupPayload,
41+
public ResponseEntity<TeamResponse> updateTeam(@PathVariable Long teamId, @RequestBody TeamPayload teamPayload,
4242
Authentication authentication) {
43-
Team team = teamService.updateTeam(teamId, groupPayload);
43+
TeamResponse team = teamService.updateTeam(teamId, teamPayload);
4444
return ResponseEntity.ok(team);
4545
}
4646

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.cf.cfteam.exceptions.codeforces;
2+
3+
import lombok.Getter;
4+
5+
@Getter
6+
public class TeamNotFoundException extends RuntimeException {
7+
8+
private final Long id;
9+
10+
public TeamNotFoundException(Long id) {
11+
super("id.not_found");
12+
this.id = id;
13+
}
14+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package com.cf.cfteam.repositories.jpa.codeforces;
22

3+
import com.cf.cfteam.models.entities.codeforces.Group;
34
import com.cf.cfteam.models.entities.codeforces.Team;
5+
import com.cf.cfteam.models.entities.security.User;
46
import org.springframework.data.jpa.repository.JpaRepository;
57

8+
import java.util.List;
9+
610
public interface TeamRepository extends JpaRepository<Team, Long> {
11+
List<Team> findByGroup(Group group);
712
}
Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
package com.cf.cfteam.services.codeforces;
22

3+
import com.cf.cfteam.exceptions.codeforces.GroupNotFoundException;
4+
import com.cf.cfteam.exceptions.codeforces.TeamNotFoundException;
5+
import com.cf.cfteam.exceptions.security.UserNotFoundException;
6+
import com.cf.cfteam.models.entities.codeforces.Group;
37
import com.cf.cfteam.models.entities.codeforces.Team;
8+
import com.cf.cfteam.models.entities.security.User;
9+
import com.cf.cfteam.repositories.jpa.codeforces.GroupRepository;
410
import com.cf.cfteam.repositories.jpa.codeforces.TeamRepository;
511
import com.cf.cfteam.transfer.payloads.codeforces.GroupPayload;
612
import com.cf.cfteam.transfer.payloads.codeforces.TeamPayload;
13+
import com.cf.cfteam.transfer.responses.codeforces.TeamResponse;
14+
import com.cf.cfteam.utils.codeforces.mappers.TeamMapper;
715
import lombok.RequiredArgsConstructor;
816
import org.springframework.stereotype.Service;
917

@@ -14,26 +22,55 @@
1422
public class TeamService {
1523

1624
private final TeamRepository teamRepository;
25+
private final GroupRepository groupRepository;
26+
private final TeamMapper teamMapper;
1727

18-
public List<Team> getAllTeamsByGroup(Long groupId) {
19-
return null;
28+
public List<TeamResponse> getAllTeamsByGroup(Long groupId) {
29+
Group group = groupRepository.findById(groupId)
30+
.orElseThrow(() -> new GroupNotFoundException(groupId));
31+
32+
List<Team> teams = teamRepository.findByGroup(group);
33+
34+
return teams.stream()
35+
.map(teamMapper::fromEntityToResponse)
36+
.toList();
2037
}
2138

22-
public Team getTeamById(Long teamId) {
23-
return teamRepository.findById(teamId).get();
39+
public TeamResponse getTeamById(Long teamId) {
40+
Team team = teamRepository.findById(teamId)
41+
.orElseThrow(() -> new TeamNotFoundException(teamId));
42+
43+
return teamMapper.fromEntityToResponse(team);
2444
}
2545

26-
public Team addTeamToGroup(Long groupId, TeamPayload teamPayload) {
27-
return null;
46+
public TeamResponse addTeamToGroup(Long groupId, TeamPayload teamPayload) {
47+
Group group = groupRepository.findById(groupId)
48+
.orElseThrow(() -> new GroupNotFoundException(groupId));
49+
50+
Team team = teamMapper.fromPayloadToEntity(teamPayload, group);
51+
team = teamRepository.save(team);
52+
53+
return teamMapper.fromEntityToResponse(team);
2854
}
2955

30-
public Team updateTeam(Long teamId, GroupPayload groupPayload) {
31-
return null;
56+
public TeamResponse updateTeam(Long teamId, TeamPayload teamPayload) {
57+
Team team = teamRepository.findById(teamId)
58+
.orElseThrow(() -> new TeamNotFoundException(teamId));
59+
60+
Team updatedTeam= teamMapper.updateEntityFromPayload(team, teamPayload);
61+
updatedTeam = teamRepository.save(updatedTeam);
62+
63+
return teamMapper.fromEntityToResponse(updatedTeam);
3264
}
3365

3466
public void deleteTeam(Long teamId) {
67+
teamRepository.deleteById(teamId);
3568
}
3669

3770
public void deleteAllTeamsByGroup(Long groupId) {
71+
Group group = groupRepository.findById(groupId)
72+
.orElseThrow(() -> new GroupNotFoundException(groupId));
73+
List<Team> teams = teamRepository.findByGroup(group);
74+
teamRepository.deleteAll(teams);
3875
}
3976
}

src/main/java/com/cf/cfteam/transfer/payloads/codeforces/PlayerPayload.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
import lombok.Builder;
44

55
@Builder
6-
public class PlayerPayload {
6+
public record PlayerPayload() {
77
}

src/main/java/com/cf/cfteam/transfer/payloads/codeforces/TeamPayload.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
import lombok.Builder;
44

55
@Builder
6-
public class TeamPayload {
6+
public record TeamPayload(
7+
String name,
8+
String description
9+
) {
710
}

src/main/java/com/cf/cfteam/utils/codeforces/mappers/TeamMapper.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.cf.cfteam.utils.codeforces.mappers;
22

3+
import com.cf.cfteam.models.entities.codeforces.Group;
34
import com.cf.cfteam.models.entities.codeforces.Team;
5+
import com.cf.cfteam.models.entities.security.User;
6+
import com.cf.cfteam.transfer.payloads.codeforces.GroupPayload;
7+
import com.cf.cfteam.transfer.payloads.codeforces.TeamPayload;
48
import com.cf.cfteam.transfer.responses.codeforces.PlayerResponse;
59
import com.cf.cfteam.transfer.responses.codeforces.TeamResponse;
610
import com.cf.cfteam.utils.codeforces.RatingCalculator;
@@ -36,4 +40,18 @@ public TeamResponse fromEntityToResponse(Team team) {
3640
.teamRating(teamRating)
3741
.build();
3842
}
43+
44+
public Team fromPayloadToEntity(TeamPayload payload, Group group) {
45+
return Team.builder()
46+
.group(group)
47+
.name(payload.name())
48+
.description(payload.description())
49+
.build();
50+
}
51+
52+
public Team updateEntityFromPayload(Team team, TeamPayload payload) {
53+
team.setName(payload.name());
54+
team.setDescription(payload.description());
55+
return team;
56+
}
3957
}

0 commit comments

Comments
 (0)