manager-service/ManagerService/Controllers/SectionAgendaController.cs

226 lines
9.2 KiB
C#

using ManagerService.Data;
using ManagerService.Data.SubSection;
using ManagerService.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using NSwag.Annotations;
using System.Collections.Generic;
using System.Linq;
using System;
using ManagerService.DTOs;
using ManagerService.Helpers;
namespace ManagerService.Controllers
{
[Authorize] // TODO Add ROLES (Roles = "Admin")
[ApiController, Route("api/[controller]")]
[OpenApiTag("Section agenda", Description = "Section agenda management")]
public class SectionAgendaController : ControllerBase
{
private readonly MyInfoMateDbContext _myInfoMateDbContext;
private readonly ILogger<SectionAgendaController> _logger;
private readonly IConfiguration _configuration;
IHexIdGeneratorService idService = new HexIdGeneratorService();
public SectionAgendaController(IConfiguration configuration, ILogger<SectionAgendaController> logger, MyInfoMateDbContext myInfoMateDbContext)
{
_logger = logger;
_configuration = configuration;
_myInfoMateDbContext = myInfoMateDbContext;
}
/// <summary>
/// Get all event from section
/// </summary>
/// <param name="sectionAgendaId">Section id</param>
[AllowAnonymous]
[ProducesResponseType(typeof(List<EventAgendaDTO>), 200)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)]
[HttpGet("{sectionAgendaId}/events")]
public ObjectResult GetAllEventAgendaFromSection(string sectionAgendaId)
{
try
{
SectionAgenda sectionAgenda = _myInfoMateDbContext.Sections.OfType<SectionAgenda>().Include(sa => sa.EventAgendas).ThenInclude(sa => sa.Resource).FirstOrDefault(sa => sa.Id == sectionAgendaId);
if (sectionAgenda == null)
throw new KeyNotFoundException("Section agenda does not exist");
/*List<ProgrammeBlock> programmeBlocks = new List<ProgrammeBlock>();
foreach (var program in sectionEvent.Programme)
{
foreach (var mapAnnotation in program.MapAnnotations) {
var resource = _myInfoMateDbContext.Resources.FirstOrDefault(r => r.Id == mapAnnotation.IconResourceId);
if (resource != null)
{
mapAnnotation.IconResource = resource; // TO check.. use DTO instead ?
}
}
}*/
return new OkObjectResult(sectionAgenda.EventAgendas.Select(ea => ea.ToDTO()));
}
catch (KeyNotFoundException ex)
{
return new NotFoundObjectResult(ex.Message) { };
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
/// <summary>
/// Create new event
/// </summary>
/// <param name="sectionAgendaId">Section agenda Id</param>
/// <param name="eventAgendaDTO">EventAgenda</param>
[ProducesResponseType(typeof(EventAgendaDTO), 200)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 409)]
[ProducesResponseType(typeof(string), 500)]
[HttpPost("{sectionAgendaId}/event")]
public ObjectResult CreateEventAgenda(string sectionAgendaId, [FromBody] EventAgendaDTO eventAgendaDTO)
{
try
{
if (sectionAgendaId == null)
throw new ArgumentNullException("Section param is null");
if (eventAgendaDTO == null)
throw new ArgumentNullException("EventAgenda param is null");
var existingSection = _myInfoMateDbContext.Sections.OfType<SectionAgenda>().Include(sa => sa.EventAgendas).FirstOrDefault(sa => sa.Id == sectionAgendaId);
if (existingSection == null)
throw new KeyNotFoundException("Section agenda does not exist");
EventAgenda eventAgenda = new EventAgenda().FromDTO(eventAgendaDTO);
_myInfoMateDbContext.EventAgendas.Add(eventAgenda);
_myInfoMateDbContext.SaveChanges();
return new OkObjectResult(eventAgenda.ToDTO());
}
catch (ArgumentNullException ex)
{
return new BadRequestObjectResult(ex.Message) { };
}
catch (InvalidOperationException ex)
{
return new ConflictObjectResult(ex.Message) { };
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
/// <summary>
/// Update an event agenda
/// </summary>
/// <param name="eventAgendaDTO">EventAgenda to update</param>
[ProducesResponseType(typeof(EventAgendaDTO), 200)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)]
[HttpPut("event")]
public ObjectResult UpdateEventAgenda([FromBody] EventAgendaDTO eventAgendaDTO)
{
try
{
if (eventAgendaDTO == null)
throw new ArgumentNullException("EventAgenda param is null");
var existing = _myInfoMateDbContext.EventAgendas.FirstOrDefault(ea => ea.Id == eventAgendaDTO.Id);
if (existing == null)
throw new KeyNotFoundException("EventAgenda does not exist");
existing.Label = eventAgendaDTO.Label;
existing.Description = eventAgendaDTO.Description;
existing.Type = eventAgendaDTO.Type;
existing.DateAdded = eventAgendaDTO.DateAdded;
existing.DateFrom = eventAgendaDTO.DateFrom;
existing.DateTo = eventAgendaDTO.DateTo;
existing.Website = eventAgendaDTO.Website;
existing.ResourceId = eventAgendaDTO.ResourceId;
existing.Phone = eventAgendaDTO.Phone;
existing.Email = eventAgendaDTO.Email;
existing.SectionAgendaId = eventAgendaDTO.SectionAgendaId;
existing.SectionEventId = eventAgendaDTO.SectionEventId;
if (eventAgendaDTO.Address != null)
{
existing.Address = new EventAddress
{
Address = eventAgendaDTO.Address.Address,
StreetNumber = eventAgendaDTO.Address.StreetNumber,
StreetName = eventAgendaDTO.Address.StreetName,
City = eventAgendaDTO.Address.City,
State = eventAgendaDTO.Address.State,
PostCode = eventAgendaDTO.Address.PostCode,
Country = eventAgendaDTO.Address.Country,
Geometry = eventAgendaDTO.Address.Geometry?.FromDto(),
PolyColor = eventAgendaDTO.Address.PolyColor,
Zoom = eventAgendaDTO.Address.Zoom
};
}
_myInfoMateDbContext.SaveChanges();
return new OkObjectResult(existing.ToDTO());
}
catch (ArgumentNullException ex)
{
return new BadRequestObjectResult(ex.Message) { };
}
catch (KeyNotFoundException ex)
{
return new NotFoundObjectResult(ex.Message) { };
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
/// <summary>
/// Delete an eventAgenda
/// </summary>
/// <param name="eventAgendaId">Id of the eventAgenda to delete</param>
[ProducesResponseType(typeof(string), 202)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)]
[HttpDelete("event/{eventAgendaId}")]
public ObjectResult DeleteEventAgenda(int eventAgendaId)
{
try
{
var eventAgenda = _myInfoMateDbContext.EventAgendas.FirstOrDefault(ea => ea.Id == eventAgendaId);
if (eventAgenda == null)
throw new KeyNotFoundException("EventAgenda does not exist");
_myInfoMateDbContext.Remove(eventAgenda);
_myInfoMateDbContext.SaveChanges();
return new ObjectResult("The eventAgenda has been deleted") { StatusCode = 202 };
}
catch (ArgumentNullException ex)
{
return new BadRequestObjectResult(ex.Message) { };
}
catch (KeyNotFoundException ex)
{
return new NotFoundObjectResult(ex.Message) { };
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
}
}