368 lines
15 KiB
C#
368 lines
15 KiB
C#
using Manager.DTOs;
|
|
using ManagerService.Data;
|
|
using ManagerService.Data.SubSection;
|
|
using ManagerService.Helpers;
|
|
using ManagerService.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using Namotion.Reflection;
|
|
using NSwag.Annotations;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using static ManagerService.Data.SubSection.SectionEvent;
|
|
|
|
namespace ManagerService.Controllers
|
|
{
|
|
[Authorize] // TODO Add ROLES (Roles = "Admin")
|
|
[ApiController, Route("api/[controller]")]
|
|
[OpenApiTag("Section event", Description = "Section event management")]
|
|
public class SectionEventController : ControllerBase
|
|
{
|
|
private readonly MyInfoMateDbContext _myInfoMateDbContext;
|
|
|
|
private readonly ILogger<SectionEventController> _logger;
|
|
private readonly IConfiguration _configuration;
|
|
IHexIdGeneratorService idService = new HexIdGeneratorService();
|
|
|
|
public SectionEventController(IConfiguration configuration, ILogger<SectionEventController> logger, MyInfoMateDbContext myInfoMateDbContext)
|
|
{
|
|
_logger = logger;
|
|
_configuration = configuration;
|
|
_myInfoMateDbContext = myInfoMateDbContext;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all programme block from section
|
|
/// </summary>
|
|
/// <param name="sectionEventId">Section id</param>
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(List<ProgrammeBlock>), 200)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("{sectionId}/programmes")]
|
|
public ObjectResult GetAllProgrammeBlockFromSection(string sectionEventId)
|
|
{
|
|
try
|
|
{
|
|
SectionEvent sectionEvent = _myInfoMateDbContext.Sections.OfType<SectionEvent>().Include(se => se.Programme).ThenInclude(se => se.MapAnnotations).ThenInclude(se => se.IconResource).FirstOrDefault(se => se.Id == sectionEventId);
|
|
|
|
if (sectionEvent == null)
|
|
throw new KeyNotFoundException("Section event 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(sectionEvent.Programme);
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new NotFoundObjectResult(ex.Message) { };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create new programme block
|
|
/// </summary>
|
|
/// <param name="sectionEventId">Section event Id</param>
|
|
/// <param name="programmeBlock">Programme block</param>
|
|
[ProducesResponseType(typeof(ProgrammeBlock), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 409)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPost("{sectionEventId}/programmes")]
|
|
public ObjectResult CreateProgrammeBlock(string sectionEventId, [FromBody] ProgrammeBlock programmeBlock)
|
|
{
|
|
try
|
|
{
|
|
if (sectionEventId == null)
|
|
throw new ArgumentNullException("Section param is null");
|
|
|
|
if (programmeBlock == null)
|
|
throw new ArgumentNullException("ProgrammeBlock param is null");
|
|
|
|
var existingSection = _myInfoMateDbContext.Sections.OfType<SectionEvent>().Include(se => se.Programme).FirstOrDefault(se => se.Id == sectionEventId);
|
|
if (existingSection == null)
|
|
throw new KeyNotFoundException("Section event does not exist");
|
|
|
|
// TODO verification ?
|
|
programmeBlock.Id = idService.GenerateHexId();
|
|
existingSection.Programme.Add(programmeBlock);
|
|
|
|
_myInfoMateDbContext.SaveChanges();
|
|
|
|
return new OkObjectResult(programmeBlock);
|
|
}
|
|
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 a program block
|
|
/// </summary>
|
|
/// <param name="programmeBlock">ProgramBlock to update</param>
|
|
[ProducesResponseType(typeof(ProgrammeBlock), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPut("programmes")]
|
|
public ObjectResult UpdateProgrammeBlock([FromBody] ProgrammeBlock programmeBlock)
|
|
{
|
|
try
|
|
{
|
|
if (programmeBlock == null)
|
|
throw new ArgumentNullException("ProgrammeBlock param is null");
|
|
|
|
var existingProgramBlock = _myInfoMateDbContext.ProgrammeBlocks.FirstOrDefault(pb => pb.Id == programmeBlock.Id);
|
|
if (existingProgramBlock == null)
|
|
throw new KeyNotFoundException("ProgrammeBlock does not exist");
|
|
|
|
existingProgramBlock = programmeBlock; // TO TEST ..
|
|
_myInfoMateDbContext.SaveChanges();
|
|
|
|
return new OkObjectResult(existingProgramBlock);
|
|
}
|
|
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 a programBlock
|
|
/// </summary>
|
|
/// <param name="programBlockId">Id of program block to delete</param>
|
|
[ProducesResponseType(typeof(string), 202)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpDelete("programmes/{programBlockId}")]
|
|
public ObjectResult DeleteProgrammeBlock(string programBlockId)
|
|
{
|
|
try
|
|
{
|
|
var programBlock = _myInfoMateDbContext.ProgrammeBlocks.Include(pb => pb.MapAnnotations).FirstOrDefault(pb => pb.Id == programBlockId);
|
|
if (programBlock == null)
|
|
throw new KeyNotFoundException("ProgramBlock does not exist");
|
|
|
|
foreach (var mapAnnotation in programBlock.MapAnnotations) // Is it really needed?
|
|
{
|
|
_myInfoMateDbContext.Remove(mapAnnotation);
|
|
}
|
|
|
|
_myInfoMateDbContext.Remove(programBlock);
|
|
_myInfoMateDbContext.SaveChanges();
|
|
|
|
return new ObjectResult("The programBlock 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 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all map annotation from program block
|
|
/// </summary>
|
|
/// <param name="programBlockId">Program block id</param>
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(List<MapAnnotation>), 200)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("{programBlockId}/map-annotations")]
|
|
public ObjectResult GetAllMapAnnotationsFromProgrammeBlock(string programBlockId)
|
|
{
|
|
try
|
|
{
|
|
ProgrammeBlock programmeBlock = _myInfoMateDbContext.ProgrammeBlocks.Include(pb => pb.MapAnnotations).ThenInclude(pb => pb.IconResource).FirstOrDefault(pb => pb.Id == programBlockId);
|
|
|
|
if (programmeBlock == null)
|
|
throw new KeyNotFoundException("ProgrammeBlock 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(programmeBlock.MapAnnotations);
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new NotFoundObjectResult(ex.Message) { };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create new map annotation
|
|
/// </summary>
|
|
/// <param name="programmeBlockId">Programme block id</param>
|
|
/// <param name="mapAnnotation">Map annotation</param>
|
|
[ProducesResponseType(typeof(MapAnnotation), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 409)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPost("{programmeBlockId}/map-annotations")]
|
|
public ObjectResult CreateMapAnnotation(string programmeBlockId, [FromBody] MapAnnotation mapAnnotation)
|
|
{
|
|
try
|
|
{
|
|
if (programmeBlockId == null)
|
|
throw new ArgumentNullException("ProgrammeBlockId param is null");
|
|
|
|
if (mapAnnotation == null)
|
|
throw new ArgumentNullException("MapAnnotation param is null");
|
|
|
|
var existingProgrammeBloc = _myInfoMateDbContext.ProgrammeBlocks.Include(pb => pb.MapAnnotations).FirstOrDefault(pb => pb.Id == programmeBlockId);
|
|
if (existingProgrammeBloc == null)
|
|
throw new KeyNotFoundException("ProgrammeBlock does not exist");
|
|
|
|
// TODO verification ?
|
|
mapAnnotation.Id = idService.GenerateHexId();
|
|
existingProgrammeBloc.MapAnnotations.Add(mapAnnotation);
|
|
|
|
_myInfoMateDbContext.SaveChanges();
|
|
|
|
return new OkObjectResult(mapAnnotation);
|
|
}
|
|
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 a map annotation
|
|
/// </summary>
|
|
/// <param name="mapAnnotation">mapAnnotation to update</param>
|
|
[ProducesResponseType(typeof(MapAnnotation), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPut("map-annotations")]
|
|
public ObjectResult UpdateMapAnnotation([FromBody] MapAnnotation mapAnnotation)
|
|
{
|
|
try
|
|
{
|
|
if (mapAnnotation == null)
|
|
throw new ArgumentNullException("MapAnnotation param is null");
|
|
|
|
var existingMapAnnotation = _myInfoMateDbContext.MapAnnotations.FirstOrDefault(ma => ma.Id == mapAnnotation.Id);
|
|
if (existingMapAnnotation == null)
|
|
throw new KeyNotFoundException("MapAnnotation does not exist");
|
|
|
|
existingMapAnnotation = mapAnnotation; // TO TEST ..
|
|
_myInfoMateDbContext.SaveChanges();
|
|
|
|
return new OkObjectResult(existingMapAnnotation);
|
|
}
|
|
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 a mapAnnotation
|
|
/// </summary>
|
|
/// <param name="mapAnnotationId">Id of map annotation to delete</param>
|
|
[ProducesResponseType(typeof(string), 202)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpDelete("map-annotations/{mapAnnotationId}")]
|
|
public ObjectResult DeleteMapAnnotation(string mapAnnotationId)
|
|
{
|
|
try
|
|
{
|
|
var mapAnnotation = _myInfoMateDbContext.MapAnnotations.FirstOrDefault(ma => ma.Id == mapAnnotationId);
|
|
if (mapAnnotation == null)
|
|
throw new KeyNotFoundException("MapAnnotation does not exist");
|
|
|
|
_myInfoMateDbContext.Remove(mapAnnotation);
|
|
_myInfoMateDbContext.SaveChanges();
|
|
|
|
return new ObjectResult("The mapAnnotation 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 };
|
|
}
|
|
}
|
|
}
|
|
}
|