Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add desk #137

Merged
merged 16 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 95 additions & 7 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 IAutoMapperConfiguration _autoMapperConfiguration;

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

/// <summary>
Expand Down Expand Up @@ -285,6 +292,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 +345,41 @@ 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">Ok</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 desk)
{
return Problem(statusCode: 501);
var adminId = RequestInteractions.ExtractIdFromRequest(Request);

try
{
_resourceUsecases.CreateDesk(desk.DeskName, desk.DeskTypeId, desk.RoomId);
return Ok();
}
catch (EntityNotFoundException e)
{
_logger.LogError(e, e.Message);
return NotFound(e.Message);
}
catch (ArgumentInvalidException e)
{
_logger.LogError(e, e.Message);
return BadRequest(e.Message);
}
catch (Exception e)
{
_logger.LogError(e, e.Message);
return Problem(statusCode: 500);
}
}

/// <summary>
Expand All @@ -370,4 +401,61 @@ 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="201"></response>
/// <response code="400"></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(CurrentDesk newDesk)
{
return Problem(statusCode: 501);
}
/// <summary>
/// Return a list of desk types
/// </summary>
/// <remarks>
/// Sample request:
/// Get /resources/desktypes with JWT-Admin Token
/// </remarks>
///
/// <response code="200">List<DeskTypDTO></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 mapper = _autoMapperConfiguration.GetConfiguration().CreateMapper();
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,8 @@ public MapperConfiguration GetConfiguration()
{
UserProfileDto.createMappings(cfg);
UserProfileCompanyDto.createMappings(cfg);
DeskTypeDto.createMappings(cfg);
ExtendedBooking.createMappings(cfg);
});
return config;
}
Expand Down
19 changes: 19 additions & 0 deletions src/deskstar-backend/Deskstar/Models/CreateDesk.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.ComponentModel.DataAnnotations;

namespace Deskstar.Models;

public class CreateDesk
{
[Required]
public Guid RoomId { get; set; }
[Required]
public Guid FloorId { get; set; }
[Required]
public Guid BuildingId { get; set; }
[Required]
public string DeskName { get; set; } = null!;

[Required]
public string DeskTyp { get; set; } = null!;

}
14 changes: 14 additions & 0 deletions src/deskstar-backend/Deskstar/Models/CreateDeskDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.ComponentModel.DataAnnotations;

namespace Deskstar.Models;

public class CreateDeskDto
{
[Required]
public Guid RoomId { get; set; }
[Required]
public string DeskName { get; set; } = null!;

[Required]
public Guid DeskTypeId { get; set; }
}
23 changes: 23 additions & 0 deletions src/deskstar-backend/Deskstar/Models/DeskTypeDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using AutoMapper;

namespace Deskstar.Models;

public class DeskTypeDto
{
public static readonly UserProfileDto Null = new UserProfileDto();

public DeskTypeDto()
{

}

public static void createMappings(IMapperConfigurationExpression cfg)
{
cfg.CreateMap<DeskTypeDto, Entities.DeskType>();
cfg.CreateMap<Entities.DeskType, DeskTypeDto>();
}

public Guid DeskTypeId { get; set; }
public string DeskTypeName { get; set; } = null!;
public Guid CompanyId { get; set; }
}
43 changes: 40 additions & 3 deletions src/deskstar-backend/Deskstar/Usecases/ResourceUsecases.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Deskstar.Core.Exceptions;
using Deskstar.DataAccess;
using Deskstar.Entities;
using Deskstar.Models;
Expand All @@ -6,6 +7,8 @@ namespace Deskstar.Usecases;

public interface IResourceUsecases
{
public List<DeskType> GetDeskTypes(Guid companyId);
public Guid CreateDesk(string deskName, Guid deskTypeId, Guid roomId);
public List<CurrentBuilding> GetBuildings(Guid userId);
public List<CurrentFloor> GetFloors(Guid buildingId);
public List<CurrentRoom> GetRooms(Guid floorId);
Expand All @@ -26,7 +29,7 @@ public ResourceUsecases(ILogger<ResourceUsecases> logger, DataContext context)

public List<CurrentBuilding> GetBuildings(Guid userId)
{

IQueryable<Building> databaseBuildings;
try
{
Expand Down Expand Up @@ -64,7 +67,7 @@ public List<CurrentFloor> GetFloors(Guid buildingId)
if (databaseFloors.ToList().Count == 0)
{
var databaseBuilding = _context.Buildings.First(building => building.BuildingId == buildingId);
if( databaseBuilding== null) throw new ArgumentException($"There is no Building with id '{buildingId}'");
if (databaseBuilding == null) throw new ArgumentException($"There is no Building with id '{buildingId}'");
}
}
catch (Exception e) when (e is FormatException or ArgumentNullException or OverflowException)
Expand Down Expand Up @@ -126,7 +129,7 @@ public List<CurrentDesk> GetDesks(Guid roomId, DateTime start, DateTime end)
if (databaseDesks.ToList().Count == 0)
{
var databaseRoom = _context.Rooms.First(room => room.RoomId == roomId);
if( databaseRoom== null) throw new ArgumentException($"There is no Room with id '{roomId}'");
if (databaseRoom == null) throw new ArgumentException($"There is no Room with id '{roomId}'");
}
mapDesksToCurrentDesks = databaseDesks.Select(desk => new CurrentDesk
{
Expand Down Expand Up @@ -205,4 +208,38 @@ public CurrentDesk GetDesk(Guid deskId, DateTime startDateTime, DateTime endDate

return mapDeskToCurrentDesk;
}

public List<DeskType> GetDeskTypes(Guid companyId)
{
return _context.DeskTypes.Where(d => d.CompanyId == companyId).ToList();
}

public Guid CreateDesk(string deskName, Guid deskTypeId, Guid roomId)
{
var desktype = _context.DeskTypes.SingleOrDefault(dt => dt.DeskTypeId == deskTypeId);
if (desktype == null)
throw new EntityNotFoundException($"There is no desk type with id '{deskTypeId}'");

var room = _context.Rooms.SingleOrDefault(r => r.RoomId == roomId);
if (room == null)
throw new EntityNotFoundException($"There is no room with id '{roomId}'");

var deskNameExists = _context.Desks.SingleOrDefault(d => d.RoomId == roomId && d.DeskName == deskName) != null;
if (deskNameExists)
throw new ArgumentInvalidException($"In this room there is already a desk named '{deskName}'");

var deskId = Guid.NewGuid();
var desk = new Desk
{
DeskId = deskId,
DeskName = deskName,
DeskTypeId = deskTypeId,
RoomId = roomId
};

_context.Desks.Add(desk);
_context.SaveChanges();

return deskId;
}
}
Loading