Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package ca.qhrtech.services.implementations; import ca.qhrtech.entities.BGLTable; import ca.qhrtech.entities.BGLUser; import ca.qhrtech.entities.Game; import ca.qhrtech.repositories.interfaces.TableRepository; import ca.qhrtech.services.interfaces.PollService; import ca.qhrtech.services.interfaces.TableService; import ca.qhrtech.services.interfaces.UserService; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * * @author bryan */ @Service("tableService") public class TableServiceImpl implements TableService { @Autowired private UserService userService; @Autowired private PollService pollService; @Autowired private TableRepository tableRepository; @Override public BGLTable findTableById(long id) { return tableRepository.findOne(id); } @Override public List<BGLTable> getTables() { return tableRepository.findByPubliclyVisible(true); } @Override public List<BGLTable> getTables(Boolean completed, LocalDateTime start, LocalDateTime end, Long creatorId) { //TODO make this fileted at the db level, not in code List<BGLTable> completeTables; if (completed != null) { completeTables = tableRepository.findByCompleted(completed); } else { completeTables = tableRepository.findByPubliclyVisible(true); } List<BGLTable> tablesInRange = Collections.EMPTY_LIST; if (start != null && end != null) { tablesInRange = tableRepository.findByDateRange(start, end); } List<BGLTable> creatorTables = Collections.EMPTY_LIST; if (creatorId != null) { creatorTables = tableRepository.findByCreator(creatorId); } Set<BGLTable> combinedTables = new HashSet<>(); combinedTables.addAll(completeTables); combinedTables.addAll(tablesInRange); combinedTables.addAll(creatorTables); return new ArrayList<>(combinedTables); } @Override public List<BGLTable> getUserTables(long userId) { return tableRepository.findByInvite(userId); } @Override public List<BGLTable> getTablesForGame(long gameId) { return tableRepository.findByGame(gameId); } @Override public List<BGLTable> getTablesInvitedTo(long userId) { return tableRepository.findByInvite(userId); } @Override public BGLTable saveTable(BGLTable table) { return tableRepository.save(table); } @Override public void updateTable(BGLTable table) { BGLTable updatedTable = tableRepository.findOne(table.getId()); table.setId(updatedTable.getId()); tableRepository.save(table); } @Override public void deleteTable(long id) { tableRepository.delete(id); } @Override public boolean doesTableExist(BGLTable table) { return findTableById(table.getId()) != null; } @Override public void addUserToTable(long tableId, long userId) { BGLTable table = findTableById(tableId); BGLUser user = userService.findUserById(userId); table.getInvites().add(user); tableRepository.save(table); } @Override public void removeUserFromTable(long tableId, long userId) { BGLTable table = findTableById(tableId); BGLUser user = userService.findUserById(userId); table.getInvites().remove(user); tableRepository.save(table); } @Override public String createPoll(long tableId, List<Game> games) { BGLTable table = findTableById(tableId); long pollId = pollService.generatePollId(games, table); table.setPollId(pollId); saveTable(table); return pollService.generatePollUrl(pollId); } }