ca.qhrtech.controllers.TableController.java Source code

Java tutorial

Introduction

Here is the source code for ca.qhrtech.controllers.TableController.java

Source

/*
 * 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.controllers;

import ca.qhrtech.entities.BGLTable;
import ca.qhrtech.entities.Game;
import ca.qhrtech.services.interfaces.GameService;
import ca.qhrtech.services.interfaces.TableService;
import ca.qhrtech.services.interfaces.UserService;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import org.jsondoc.core.annotation.Api;
import org.jsondoc.core.annotation.ApiMethod;
import org.jsondoc.core.pojo.ApiStage;
import org.jsondoc.core.pojo.ApiVisibility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author bryan
 */
@Api(name = "Table Services", description = "Methods for managing tables", group = "Games", visibility = ApiVisibility.PUBLIC, stage = ApiStage.ALPHA)
@RestController
public class TableController {

    @Autowired
    private TableService tableService;

    @Autowired
    private UserService userService;

    @Autowired
    private GameService gameService;

    @RequestMapping("/table")
    public List<BGLTable> getPublicTables(@RequestParam(value = "complete", required = false) Boolean complete,
            @RequestParam(value = "start", required = false) @DateTimeFormat(pattern = "MMddyyyy") LocalDateTime start,
            @RequestParam(value = "end", required = false) @DateTimeFormat(pattern = "MMddyyyy") LocalDateTime end,
            @RequestParam(value = "creatorId", required = false) Long creatorId) {

        return tableService.getTables(complete, start, end, creatorId);
    }

    @RequestMapping("/table/user/{id}")
    public ResponseEntity<List<BGLTable>> getUserTables(@PathVariable("id") long id) {
        if (userService.findUserById(id) == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(tableService.getUserTables(id), HttpStatus.OK);
    }

    @RequestMapping("/table/game/{id}")
    public ResponseEntity<List<BGLTable>> getTablesForGame(@PathVariable("id") long id) {
        if (gameService.findGameById(id) == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(tableService.getTablesForGame(id), HttpStatus.OK);
    }

    @RequestMapping("/table/{id}")
    public BGLTable getTable(@PathVariable("id") long id) {
        return tableService.findTableById(id);
    }

    @RequestMapping(value = "/table", method = RequestMethod.POST)
    public ResponseEntity<BGLTable> createTable(@RequestBody BGLTable table) {
        if (!tableService.doesTableExist(table)) {
            BGLTable newTable = tableService.saveTable(table);
            return new ResponseEntity<>(newTable, HttpStatus.CREATED);
        }
        return new ResponseEntity<>(HttpStatus.CONFLICT);
    }

    @RequestMapping(value = "/table/{id}", method = RequestMethod.PUT)
    public ResponseEntity<BGLTable> updateTable(@PathVariable("id") long id, @RequestBody BGLTable table) {
        BGLTable currentTable = tableService.findTableById(id);
        if (currentTable == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        currentTable.setCompleted(table.isCompleted());
        currentTable.setGame(table.getGame());
        currentTable.setInvites(new ArrayList<>(table.getInvites()));
        currentTable.setPollId(table.getPollId());
        currentTable.setPubliclyVisible(table.isPubliclyVisible());
        currentTable.setStartDate(table.getStartDate());
        tableService.updateTable(currentTable);
        return new ResponseEntity<>(currentTable, HttpStatus.OK);

    }

    @RequestMapping(value = "/table/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<BGLTable> deleteTable(@PathVariable("id") long id) {
        if (tableService.findTableById(id) == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        tableService.deleteTable(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @RequestMapping(value = "/table/{tableId}/user/{userId}", method = RequestMethod.PUT)
    public ResponseEntity<BGLTable> inviteUser(@PathVariable("tableId") long tableId,
            @PathVariable("userId") long userId) {
        if (tableService.findTableById(tableId) == null || userService.findUserById(userId) == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        tableService.addUserToTable(tableId, userId);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @RequestMapping(value = "/table/{tableId}/user/{userId}", method = RequestMethod.DELETE)
    public ResponseEntity<BGLTable> uninviteUser(@PathVariable("tableId") long tableId,
            @PathVariable("userId") long userId) {
        if (tableService.findTableById(tableId) == null || userService.findUserById(userId) == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        tableService.removeUserFromTable(tableId, userId);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiMethod(description = "Generates a 3rd Party Poll to determine the Game for this Table")
    @RequestMapping(value = "/table/{tableId}/poll", method = RequestMethod.PUT)
    public ResponseEntity<String> createPollForTable(@PathVariable("tableId") long tableId,
            @RequestBody List<Game> games) {
        if (tableService.findTableById(tableId) == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        for (Game game : games) {
            if (gameService.findGameById(game.getId()) == null) {
                return new ResponseEntity<>(HttpStatus.NOT_FOUND);
            }
        }
        //TODO validate for game limit (based on poll implementation, e.g. 30 for straw poll)
        //TODO validate for poll api limits (e.g. 100 polls per 30 mins for straw poll)
        String pollUrl = tableService.createPoll(tableId, games);
        return new ResponseEntity<>(pollUrl, HttpStatus.OK);
    }

}