using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using AutoMapper; using AutoMapper.QueryableExtensions; using IdentityServer4.Extensions; using InternshipSystem.Api.Result; using InternshipSystem.Api.Security; using InternshipSystem.Repository; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace InternshipSystem.Api.Controllers { [Route("editions")] public class EditionController : ControllerBase { private InternshipDbContext Context { get; } private IMapper Mapper { get; } public EditionController(InternshipDbContext context, IMapper mapper) { Context = context; Mapper = mapper; } /// /// Get accessible editions /// /// Editions accessible by the current user /// This action is only available for authorized student /// [HttpGet] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [Authorize] public async Task>> GetAvailableEditions([FromServices] User user, CancellationToken token) { var editions = await Context.Editions .Where(edition => edition.Internships .Any(internship => internship.Student.Id == user.PersonNumber)) .ProjectTo(Mapper.ConfigurationProvider) .ToListAsync(token); if (editions.IsNullOrEmpty()) { return NotFound(); } return Ok(editions); } /// /// Get accessible editions /// /// Editions accessible by the current user /// This action is only available for authorized student /// /// [HttpGet("{id}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task> GetEditionInfo(Guid id, CancellationToken token) { var edition = await Context.Editions .Where(e => e.Id == id) .ProjectTo(Mapper.ConfigurationProvider) .FirstOrDefaultAsync(token); if (edition == null) { return NotFound(); } return Ok(edition); } /// /// Get current edition's configuration /// /// Parameters of edition registered for by student /// This action is only available for authorized student registered for this edition edition /// Specified edition doesn't exist /// [HttpGet("current")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [Authorize(Policy = Policies.RegisteredOnly)] public async Task> GetEditionsConfiguration([FromServices] User user, CancellationToken token) { var edition = await Context.Editions .Include(e => e.AvailableSubjects) .Include(e => e.Course) .Where(e => e.Id == user.EditionId) .ProjectTo(Mapper.ConfigurationProvider) .FirstOrDefaultAsync(token); if (edition == null) { return NotFound(); } return Ok(edition); } } }