Skip to content

Commit

Permalink
Refactor with team repository
Browse files Browse the repository at this point in the history
  • Loading branch information
GODrums committed Nov 15, 2024
1 parent a6f388b commit ec3e103
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public ResponseEntity<Set<String>> updateRepositories(@RequestBody List<String>
}

@GetMapping("/users")
public ResponseEntity<List<UserTeamsDTO>> getUsersAsAdmin() {
return ResponseEntity.ok(adminService.getUsersAsAdmin());
public ResponseEntity<List<UserTeamsDTO>> getUsersWithTeams() {
return ResponseEntity.ok(adminService.getUsersWithTeams());
}

@PutMapping("/user/{login}/team/{teamId}")
Expand All @@ -70,8 +70,8 @@ public ResponseEntity<UserInfoDTO> addTeamToUser(@PathVariable String login, @Pa
}

@DeleteMapping("/user/{login}/team/{teamId}")
public ResponseEntity<UserInfoDTO> removeTeamFromUser(@PathVariable String login, @PathVariable Long teamId) {
return adminService.removeTeamFromUser(login, teamId)
public ResponseEntity<UserInfoDTO> removeUserFromTeam(@PathVariable String login, @PathVariable Long teamId) {
return adminService.removeUserFromTeam(login, teamId)
.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.notFound().build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import de.tum.in.www1.hephaestus.gitprovider.repository.RepositoryRepository;
import de.tum.in.www1.hephaestus.gitprovider.team.Team;
import de.tum.in.www1.hephaestus.gitprovider.team.TeamInfoDTO;
import de.tum.in.www1.hephaestus.gitprovider.team.TeamRepository;
import de.tum.in.www1.hephaestus.gitprovider.team.TeamService;
import de.tum.in.www1.hephaestus.gitprovider.user.User;
import de.tum.in.www1.hephaestus.gitprovider.user.UserInfoDTO;
Expand Down Expand Up @@ -35,6 +36,9 @@ public class AdminService {
@Autowired
private TeamService teamService;

@Autowired
private TeamRepository teamRepository;

@Autowired
private RepositoryRepository repositoryRepository;

Expand All @@ -47,7 +51,7 @@ public class AdminService {
private AdminConfig createInitialConfig() {
AdminConfig newAdminConfig = new AdminConfig();
newAdminConfig.setRepositoriesToMonitor(Set.of(repositoriesToMonitor));
return adminRepository.saveAndFlush(newAdminConfig);
return adminRepository.save(newAdminConfig);
}

@Cacheable("config")
Expand All @@ -65,7 +69,7 @@ public Set<String> updateRepositories(Set<String> repositories) {
return adminConfig.getRepositoriesToMonitor();
}

public List<UserTeamsDTO> getUsersAsAdmin() {
public List<UserTeamsDTO> getUsersWithTeams() {
logger.info("Getting all users with their teams");
return userRepository.findAll().stream().map(UserTeamsDTO::fromUser).toList();
}
Expand All @@ -76,31 +80,31 @@ public Optional<UserInfoDTO> addTeamToUser(String login, Long teamId) {
if (optionalUser.isEmpty()) {
return Optional.empty();
}
Optional<Team> optionalTeam = teamService.getTeam(teamId);
Optional<Team> optionalTeam = teamRepository.findById(teamId);
if (optionalTeam.isEmpty()) {
return Optional.empty();
}
Team team = optionalTeam.get();
User user = optionalUser.get();
team.addMember(user);
teamService.saveTeam(team);
teamRepository.save(team);
return Optional.of(UserInfoDTO.fromUser(user));
}

public Optional<UserInfoDTO> removeTeamFromUser(String login, Long teamId) {
public Optional<UserInfoDTO> removeUserFromTeam(String login, Long teamId) {
logger.info("Removing team (ID: " + teamId + ") from user with login: " + login);
Optional<User> optionalUser = userRepository.findByLogin(login);
if (optionalUser.isEmpty()) {
return Optional.empty();
}
Optional<Team> optionalTeam = teamService.getTeam(teamId);
Optional<Team> optionalTeam = teamRepository.findById(teamId);
if (optionalTeam.isEmpty()) {
return Optional.empty();
}
Team team = optionalTeam.get();
User user = optionalUser.get();
team.removeMember(user);
teamService.saveTeam(team);
teamRepository.save(team);
return Optional.of(UserInfoDTO.fromUser(user));
}

Expand All @@ -111,31 +115,31 @@ public TeamInfoDTO createTeam(String name, String color) {

public Optional<TeamInfoDTO> addRepositoryToTeam(Long teamId, String repositoryName) {
logger.info("Adding repository with name: " + repositoryName + " to team with ID: " + teamId);
Optional<Team> optionalTeam = teamService.getTeam(teamId);
Optional<Team> optionalTeam = teamRepository.findById(teamId);
if (optionalTeam.isEmpty()) {
return Optional.empty();
}
Team team = optionalTeam.get();
repositoryRepository.findByNameWithOwner(repositoryName).ifPresent(team::addRepository);
teamService.saveTeam(team);
teamRepository.save(team);
return Optional.of(TeamInfoDTO.fromTeam(team));
}

public Optional<TeamInfoDTO> removeRepositoryFromTeam(Long teamId, String repositoryName) {
logger.info("Removing repository with name: " + repositoryName + " from team with ID: " + teamId);
Optional<Team> optionalTeam = teamService.getTeam(teamId);
Optional<Team> optionalTeam = teamRepository.findById(teamId);
if (optionalTeam.isEmpty()) {
return Optional.empty();
}
Team team = optionalTeam.get();
repositoryRepository.findByNameWithOwner(repositoryName).ifPresent(team::removeRepository);
teamService.saveTeam(team);
teamRepository.save(team);
return Optional.of(TeamInfoDTO.fromTeam(team));
}

public Optional<TeamInfoDTO> addLabelToTeam(Long teamId, String label) {
logger.info("Adding label '" + label + "' to team with ID: " + teamId);
Optional<Team> optionalTeam = teamService.getTeam(teamId);
Optional<Team> optionalTeam = teamRepository.findById(teamId);
if (optionalTeam.isEmpty()) {
return Optional.empty();
}
Expand All @@ -145,13 +149,13 @@ public Optional<TeamInfoDTO> addLabelToTeam(Long teamId, String label) {
return Optional.empty();
}
team.addLabel(labelEntity.get());
teamService.saveTeam(team);
teamRepository.save(team);
return Optional.of(TeamInfoDTO.fromTeam(team));
}

public Optional<TeamInfoDTO> removeLabelFromTeam(Long teamId, String label) {
logger.info("Removing label '" + label + "' from team with ID: " + teamId);
Optional<Team> optionalTeam = teamService.getTeam(teamId);
Optional<Team> optionalTeam = teamRepository.findById(teamId);
if (optionalTeam.isEmpty()) {
return Optional.empty();
}
Expand All @@ -161,17 +165,17 @@ public Optional<TeamInfoDTO> removeLabelFromTeam(Long teamId, String label) {
return Optional.empty();
}
team.removeLabel(labelEntity.get());
teamService.saveTeam(team);
teamRepository.save(team);
return Optional.of(TeamInfoDTO.fromTeam(team));
}

public Optional<TeamInfoDTO> deleteTeam(Long teamId) {
logger.info("Deleting team with ID: " + teamId);
Optional<Team> optionalTeam = teamService.getTeam(teamId);
Optional<Team> optionalTeam = teamRepository.findById(teamId);
if (optionalTeam.isEmpty()) {
return Optional.empty();
}
teamService.deleteTeam(teamId);
teamRepository.delete(optionalTeam.get());
return Optional.of(TeamInfoDTO.fromTeam(optionalTeam.get()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import jakarta.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -23,22 +22,12 @@ public class TeamService {
@Autowired
private LabelRepository labelRepository;

public Optional<Team> getTeam(Long id) {
logger.info("Getting team with id: " + id);
return teamRepository.findById(id);
}

@Transactional
public List<TeamInfoDTO> getAllTeams() {
List<TeamInfoDTO> teams = teamRepository.findAll().stream().map(TeamInfoDTO::fromTeam).toList();
return teams;
}

public Team saveTeam(Team team) {
logger.info("Saving team: " + team);
return teamRepository.saveAndFlush(team);
}

public Team createTeam(String name, String color) {
logger.info("Creating team with name: " + name + " and color: " + color);
Team team = new Team();
Expand All @@ -47,12 +36,6 @@ public Team createTeam(String name, String color) {
return teamRepository.saveAndFlush(team);
}

public void deleteTeam(Long id) {
logger.info("Deleting team with id: " + id);
teamRepository.deleteById(id);
teamRepository.flush();
}

@Transactional
public void syncDefaultTeams() {
logger.info("Creating default teams");
Expand Down

0 comments on commit ec3e103

Please sign in to comment.