Skip to content

Commit

Permalink
Merge pull request #137 from amosproj/54-add-desk
Browse files Browse the repository at this point in the history
feat: add desk
  • Loading branch information
stefanpfahler authored Jan 5, 2023
2 parents d4ac171 + 4423032 commit 58d1387
Show file tree
Hide file tree
Showing 29 changed files with 1,862 additions and 91 deletions.
240 changes: 210 additions & 30 deletions src/deskstar-backend/Deskstar/Controllers/ResourcesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Deskstar.Usecases;
using Deskstar.Models;
using Deskstar.Core;
using Deskstar.Entities;
using Deskstar.Core.Exceptions;

namespace Deskstar.Controllers;

Expand All @@ -12,12 +14,17 @@ namespace Deskstar.Controllers;
public class ResourcesController : ControllerBase
{
private readonly IResourceUsecases _resourceUsecases;
private readonly IUserUsecases _userUsecases;
private readonly ILogger<ResourcesController> _logger;

public ResourcesController(ILogger<ResourcesController> logger, IResourceUsecases resourceUsecases)
private readonly AutoMapper.IMapper _mapper;

public ResourcesController(ILogger<ResourcesController> logger, IResourceUsecases resourceUsecases, IUserUsecases userUsecases, IAutoMapperConfiguration autoMapperConfiguration)
{
_logger = logger;
_resourceUsecases = resourceUsecases;
_userUsecases = userUsecases;
_mapper = autoMapperConfiguration.GetConfiguration().CreateMapper();
}

/// <summary>
Expand All @@ -30,6 +37,7 @@ public ResourcesController(ILogger<ResourcesController> logger, IResourceUsecase
/// </remarks>
///
/// <response code="200">Returns the buildings list</response>
/// <response code="400">Bad Request</response>
/// <response code="500">Internal Server Error</response>
[HttpGet("buildings")]
[Authorize]
Expand Down Expand Up @@ -60,17 +68,43 @@ public IActionResult GetAllBuildings()
/// POST /resources/buildings with JWT-Admin Token
/// </remarks>
///
/// <response code="205"></response>
/// <response code="200">CreateBuildingResponseObject</response>
/// <response code="400">Bad Request</response>
/// <response code="404">Not Found</response>
/// <response code="500">Internal Server Error</response>
[HttpPost("buildings")]
[Authorize(Policy = "Admin")]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[Produces("application/json")]
public IActionResult CreateBuilding(string buildingId)
public IActionResult CreateBuilding(CreateBuildingDto buildingDto)
{
return Problem(statusCode: 501);
var adminId = RequestInteractions.ExtractIdFromRequest(Request);

try
{
var companyId = _userUsecases.ReadSpecificUser(adminId).CompanyId;
var building = _resourceUsecases.CreateBuilding(buildingDto.BuildingName, buildingDto.Location, companyId);
var resultBuilding = _mapper.Map<Building, CreateBuildingResponseObject>(building);
return Ok(resultBuilding);
}
catch (EntityNotFoundException e)
{
_logger.LogError(e, e.Message);
return NotFound(e.Message);
}
catch (Exception e) when (e is ArgumentInvalidException or ArgumentNullException or FormatException or OverflowException)
{
_logger.LogError(e, e.Message);
return BadRequest(e.Message);
}
catch (Exception e)
{
_logger.LogError(e, e.Message);
return Problem(statusCode: 500);
}
}

/// <summary>
Expand All @@ -81,8 +115,8 @@ public IActionResult CreateBuilding(string buildingId)
/// DELETE /resources/buildings/3de7afbf-0289-4ba6-bada-a34353c5548a with JWT-Admin Token
/// </remarks>
///
/// <response code="205"></response>
/// <response code="500">Internal Server Error</response>
/// <response code="400">Bad Request</response>
/// <response code="501">Not Implemented</response>
[HttpDelete("buildings/{buildingId}")]
[Authorize(Policy = "Admin")]
[ProducesResponseType(StatusCodes.Status205ResetContent)]
Expand All @@ -104,6 +138,7 @@ public IActionResult DeleteBuilding(string buildingId)
/// </remarks>
///
/// <response code="200">Returns the floor list</response>
/// <response code="400">Bad Request</response>
/// <response code="500">Internal Server Error</response>
[HttpGet("buildings/{buildingId}/floors")]
[Authorize]
Expand Down Expand Up @@ -133,18 +168,42 @@ public IActionResult GetFloorsByBuildingId(string buildingId)
/// POST /resources/floors with JWT-Admin Token
/// </remarks>
///
/// <response code="201"></response>
/// <response code="409"></response>
/// <response code="200">CreateFloorResponseObject</response>
/// <response code="400">Bad Request</response>
/// <response code="404">Not Found</response>
/// <response code="500">Internal Server Error</response>
[HttpPost("floors")]
[Authorize(Policy = "Admin")]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[Produces("application/json")]
public IActionResult CreateFloor(CurrentFloor newFloor)
public IActionResult CreateFloor(CreateFloorDto floorDto)
{
return Problem(statusCode: 501);
var adminId = RequestInteractions.ExtractIdFromRequest(Request);

try
{
var buildingId = new Guid(floorDto.BuildingId);
var floor = _resourceUsecases.CreateFloor(floorDto.FloorName, buildingId);
var resultFloor = _mapper.Map<Floor, CreateFloorResponseObject>(floor);
return Ok(resultFloor);
}
catch (EntityNotFoundException e)
{
_logger.LogError(e, e.Message);
return NotFound(e.Message);
}
catch (Exception e) when (e is ArgumentInvalidException or ArgumentNullException or FormatException or OverflowException)
{
_logger.LogError(e, e.Message);
return BadRequest(e.Message);
}
catch (Exception e)
{
_logger.LogError(e, e.Message);
return Problem(statusCode: 500);
}
}

/// <summary>
Expand All @@ -155,8 +214,8 @@ public IActionResult CreateFloor(CurrentFloor newFloor)
/// DELETE /resources/floors/3de7afbf-0289-4ba6-bada-a34353c5548a with JWT-Admin Token
/// </remarks>
///
/// <response code="205"></response>
/// <response code="500">Internal Server Error</response>
/// <response code="400">Bad Request</response>
/// <response code="501">Not Impelemented</response>
[HttpDelete("floors/{floorId}")]
[Authorize(Policy = "Admin")]
[ProducesResponseType(StatusCodes.Status205ResetContent)]
Expand All @@ -177,6 +236,7 @@ public IActionResult DeleteFloor(string floorId)
/// </remarks>
///
/// <response code="200">Returns the rooms list</response>
/// <response code="400">Bad Request</response>
/// <response code="500">Internal Server Error</response>
[HttpGet("floors/{floorId}/rooms")]
[Authorize]
Expand Down Expand Up @@ -206,18 +266,43 @@ public IActionResult GetRoomsByFloorId(string floorId)
/// POST /resources/rooms with JWT-Admin Token
/// </remarks>
///
/// <response code="201"></response>
/// <response code="409"></response>
/// <response code="200">CreateRoomResponseObject</response>
/// <response code="400">Bad Request</response>
/// <response code="404">Not Found</response>
/// <response code="500">Internal Server Error</response>
[HttpPost("rooms")]
[Authorize(Policy = "Admin")]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[Produces("application/json")]
public IActionResult CreateRoom(CurrentRoom newRoom)
public IActionResult CreateRoom(CreateRoomDto roomDto)
{
return Problem(statusCode: 501);
var adminId = RequestInteractions.ExtractIdFromRequest(Request);

try
{
var floorId = new Guid(roomDto.FloorId);
var room = _resourceUsecases.CreateRoom(roomDto.RoomName, floorId);
var resultRoom = _mapper.Map<Room, CreateRoomResponseObject>(room);
return Ok(resultRoom);
}
catch (EntityNotFoundException e)
{
_logger.LogError(e, e.Message);
return NotFound(e.Message);
}
catch (Exception e) when (e is ArgumentInvalidException or ArgumentNullException or FormatException or OverflowException)
{
_logger.LogError(e, e.Message);
return BadRequest(e.Message);
}
catch (Exception e)
{
_logger.LogError(e, e.Message);
return Problem(statusCode: 500);
}
}

/// <summary>
Expand All @@ -228,8 +313,8 @@ public IActionResult CreateRoom(CurrentRoom newRoom)
/// DELETE /resources/rooms/3de7afbf-0289-4ba6-bada-a34353c5548a with JWT-Admin Token
/// </remarks>
///
/// <response code="205"></response>
/// <response code="500">Internal Server Error</response>
/// <response code="400">Bad Request</response>
/// <response code="501">Not Implemented</response>
[HttpDelete("rooms/{roomId}")]
[Authorize(Policy = "Admin")]
[ProducesResponseType(StatusCodes.Status205ResetContent)]
Expand All @@ -251,6 +336,7 @@ public IActionResult DeleteRoom(string roomId)
/// </remarks>
///
/// <response code="200">Returns the desks list</response>
/// <response code="400">Bad Request</response>
/// <response code="500">Internal Server Error</response>
[HttpGet("rooms/{roomId}/desks")]
[Authorize]
Expand Down Expand Up @@ -285,6 +371,7 @@ public IActionResult GetDesksByRoomId(string roomId, long start = 0, long end =
/// </remarks>
///
/// <response code="200">Returns the buildings list</response>
/// <response code="400">Bad Request</response>
/// <response code="500">Internal Server Error</response>
[HttpGet("desks/{deskId}")]
[Authorize]
Expand Down Expand Up @@ -337,18 +424,44 @@ public IActionResult GetDeskDetailsByDeskId(string deskId, long start = 0, long
/// POST /resources/desks with JWT-Admin Token
/// </remarks>
///
/// <response code="201"></response>
/// <response code="409"></response>
/// <response code="200">CreateDeskResponseObject</response>
/// <response code="400">Bad Request</response>
/// <response code="404">Not Found</response>
/// <response code="500">Internal Server Error</response>
[HttpPost("desks")]
[Authorize(Policy = "Admin")]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[Produces("application/json")]
public IActionResult CreateDesk(CurrentDesk newDesk)
public IActionResult CreateDesk(CreateDeskDto deskDto)
{
return Problem(statusCode: 501);
var adminId = RequestInteractions.ExtractIdFromRequest(Request);

try
{
var deskTypeId = new Guid(deskDto.DeskTypeId);
var roomId = new Guid(deskDto.RoomId);
var desk = _resourceUsecases.CreateDesk(deskDto.DeskName, deskTypeId, roomId);
var resultDesk = _mapper.Map<Desk, CreateDeskResponseObject>(desk);
return Ok(resultDesk);
}
catch (EntityNotFoundException e)
{
_logger.LogError(e, e.Message);
return NotFound(e.Message);
}
catch (Exception e) when (e is ArgumentInvalidException or ArgumentNullException or FormatException or OverflowException)
{
_logger.LogError(e, e.Message);
return BadRequest(e.Message);
}
catch (Exception e)
{
_logger.LogError(e, e.Message);
return Problem(statusCode: 500);
}
}

/// <summary>
Expand All @@ -359,8 +472,8 @@ public IActionResult CreateDesk(CurrentDesk newDesk)
/// DELETE /resources/desks/3de7afbf-0289-4ba6-bada-a34353c5548a with JWT-Admin Token
/// </remarks>
///
/// <response code="205"></response>
/// <response code="500">Internal Server Error</response>
/// <response code="400">Bad Request</response>
/// <response code="501">Not Implemented</response>
[HttpDelete("desks/{deskId}")]
[Authorize(Policy = "Admin")]
[ProducesResponseType(StatusCodes.Status205ResetContent)]
Expand All @@ -370,4 +483,71 @@ public IActionResult DeleteDesk(string deskId)
{
return Problem(statusCode: 501);
}

/// <summary>
/// Creates a new DeskType.
/// </summary>
/// <remarks>
/// Sample request:
/// POST /resources/desktypes with JWT-Admin Token
/// </remarks>
///
/// <response code="200">CreateDeskTypeResponseObject</response>
/// <response code="400">Bad Request</response>
/// <response code="500">Internal Server Error</response>
[HttpPost("desktypes")]
[Authorize(Policy = "Admin")]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[Produces("application/json")]
public IActionResult CreateDeskType(CreateDeskTypeDto deskTypeDto)
{
var adminId = RequestInteractions.ExtractIdFromRequest(Request);
try
{
var companyId = _userUsecases.ReadSpecificUser(adminId).CompanyId;
var deskType = _resourceUsecases.CreateDeskType(deskTypeDto.DeskTypeName, companyId);
var resultDeskType = _mapper.Map<DeskType, CreateDeskTypeResponseObject>(deskType);
return Ok(resultDeskType);
}
catch (Exception e)
{
_logger.LogError(e, e.Message);
return Problem(statusCode: 500);
}
}
/// <summary>
/// Return a list of desk types
/// </summary>
/// <remarks>
/// Sample request:
/// Get /resources/desktypes with JWT-Admin Token
/// </remarks>
///
/// <response code="200">List<DeskTypeDto></response>
/// <response code="400">Bad Request</response>
/// <response code="500">Internal Server Error</response>
[HttpGet("desktypes")]
[Authorize(Policy = "Admin")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[Produces("application/json")]
public IActionResult ReadDeskTypes()
{
var adminId = RequestInteractions.ExtractIdFromRequest(Request);

try
{
var companyId = _userUsecases.ReadSpecificUser(adminId).CompanyId;
var entities = _resourceUsecases.GetDeskTypes(companyId);
var deskTypes = entities.Select<DeskType, DeskTypeDto>(desktype => _mapper.Map<DeskType, DeskTypeDto>(desktype)).ToList();
return Ok(deskTypes);
}
catch (Exception e)
{
_logger.LogError(e, e.Message);
return Problem(statusCode: 500);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ public MapperConfiguration GetConfiguration()
{
UserProfileDto.createMappings(cfg);
UserProfileCompanyDto.createMappings(cfg);
DeskTypeDto.createMappings(cfg);
ExtendedBooking.createMappings(cfg);
CreateBuildingResponseObject.createMappings(cfg);
CreateDeskResponseObject.createMappings(cfg);
CreateDeskTypeResponseObject.createMappings(cfg);
CreateFloorResponseObject.createMappings(cfg);
CreateRoomResponseObject.createMappings(cfg);
});
return config;
}
Expand Down
Loading

0 comments on commit 58d1387

Please sign in to comment.