using System.Linq; using System.Threading; using System.Threading.Tasks; using InternshipSystem.Api.Security; using InternshipSystem.Core; using InternshipSystem.Repository; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace InternshipSystem.Api.Controllers { [ApiController] [Route("management/document")] public class DocumentManagementController : ControllerBase { private InternshipDbContext Context { get; } public DocumentManagementController(InternshipDbContext context) { Context = context; } [HttpDelete("{documentId}/delete")] [Authorize(Policy = Policies.IsOverseer)] public async Task DeleteDocument(long documentId, CancellationToken ct) { var internship = await Context.Internships .Include(i => i.Documentation) .FirstOrDefaultAsync(i => i.Documentation.Any(d => d.Id.Equals(documentId)), ct); if (internship == null) { return NotFound(); } internship.RemoveDocument(documentId); await Context.SaveChangesAsync(ct); return Ok(); } [HttpPut("{documentId}/accept")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status404NotFound)] [Authorize(Policy = Policies.IsOverseer)] public async Task AcceptDocument(long documentId, [FromBody] string comment, CancellationToken token) { var internship = await Context.Internships .Include(i => i.Documentation) .FirstOrDefaultAsync(i => i.Documentation.Any(d => d.Id.Equals(documentId)), token); if (internship == null) { return NotFound(); } var document = internship.Documentation .FirstOrDefault(d => d.Id.Equals(documentId)); if (document == null) { return NotFound(); } document.State = DocumentState.Accepted; document.ChangeStateComment = string.IsNullOrEmpty(comment) ? null : comment; await Context.SaveChangesAsync(token); return Ok(); } [HttpPut("{documentId}/reject")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status404NotFound)] [Authorize(Policy = Policies.IsOverseer)] public async Task RejectDocument(long documentId, [FromBody] string comment, CancellationToken token) { var internship = await Context.Internships .Include(i => i.Documentation) .FirstOrDefaultAsync(i => i.Documentation.Any(d => d.Id.Equals(documentId)), token); if (internship == null) { return NotFound(); } var document = internship.Documentation .FirstOrDefault(d => d.Id.Equals(documentId)); if (document == null) { return NotFound(); } document.State = DocumentState.Rejected; document.ChangeStateComment = string.IsNullOrEmpty(comment) ? null : comment; await Context.SaveChangesAsync(token); return Ok(); } } }