226 lines
9.2 KiB
C#
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 };
|
|
}
|
|
}
|
|
}
|
|
}
|