Edition Create/Edit
This commit is contained in:
parent
74e3957274
commit
78707bf3c5
@ -21,7 +21,7 @@ namespace InternshipSystem.Api
|
|||||||
|
|
||||||
CreateMap<Edition, EditionManagementResult>();
|
CreateMap<Edition, EditionManagementResult>();
|
||||||
|
|
||||||
CreateMap<Edition, EditionFullResult>();
|
CreateMap<Edition, EditionDetailsResult>();
|
||||||
|
|
||||||
CreateMap<Edition, EditionConfigurationResult>();
|
CreateMap<Edition, EditionConfigurationResult>();
|
||||||
|
|
||||||
|
@ -5,9 +5,11 @@ using System.Threading;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
using AutoMapper.QueryableExtensions;
|
using AutoMapper.QueryableExtensions;
|
||||||
|
using InternshipSystem.Api.Queries;
|
||||||
using InternshipSystem.Api.Queries.SearchQuery;
|
using InternshipSystem.Api.Queries.SearchQuery;
|
||||||
using InternshipSystem.Api.Result;
|
using InternshipSystem.Api.Result;
|
||||||
using InternshipSystem.Api.Security;
|
using InternshipSystem.Api.Security;
|
||||||
|
using InternshipSystem.Core;
|
||||||
using InternshipSystem.Repository;
|
using InternshipSystem.Repository;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
@ -46,7 +48,7 @@ namespace InternshipSystem.Api.Controllers
|
|||||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
[Authorize(Policy = Policies.IsOverseer)]
|
[Authorize(Policy = Policies.IsOverseer)]
|
||||||
public async Task<ActionResult<EditionFullResult>> GetFullEdition(CancellationToken token)
|
public async Task<ActionResult<EditionDetailsResult>> GetFullEdition(Guid editionId, CancellationToken token)
|
||||||
{
|
{
|
||||||
var edition = await Context.Editions
|
var edition = await Context.Editions
|
||||||
.Include(e => e.Course)
|
.Include(e => e.Course)
|
||||||
@ -54,7 +56,8 @@ namespace InternshipSystem.Api.Controllers
|
|||||||
.ThenInclude(s => s.Subject)
|
.ThenInclude(s => s.Subject)
|
||||||
.Include(e => e.AvailableInternshipTypes)
|
.Include(e => e.AvailableInternshipTypes)
|
||||||
.ThenInclude(i => i.InternshipType)
|
.ThenInclude(i => i.InternshipType)
|
||||||
.ProjectTo<EditionFullResult>(Mapper.ConfigurationProvider)
|
.Where(e => e.Id == editionId)
|
||||||
|
.ProjectTo<EditionDetailsResult>(Mapper.ConfigurationProvider)
|
||||||
.FirstOrDefaultAsync(token);
|
.FirstOrDefaultAsync(token);
|
||||||
|
|
||||||
if (edition == null)
|
if (edition == null)
|
||||||
@ -68,22 +71,58 @@ namespace InternshipSystem.Api.Controllers
|
|||||||
[HttpPut]
|
[HttpPut]
|
||||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
[Authorize(Policy = Policies.IsOverseer)]
|
[Authorize(Policy = Policies.IsOverseer)]
|
||||||
public async Task<ActionResult> UpsertEdition(EditionForm editionForm, CancellationToken token)
|
public async Task<ActionResult> UpsertEdition(EditionForm editionForm, CancellationToken token)
|
||||||
{
|
{
|
||||||
var validator = new EditionForm.Validator();
|
var validator = new EditionForm.Validator();
|
||||||
// TODO: complete validation rules
|
|
||||||
var validationResult = await validator.ValidateAsync(editionForm, token);
|
var validationResult = await validator.ValidateAsync(editionForm, token);
|
||||||
|
|
||||||
if (!validationResult.IsValid)
|
if (!validationResult.IsValid)
|
||||||
{
|
{
|
||||||
return BadRequest(validationResult.ToString());
|
return BadRequest(validationResult.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: complete add/update
|
|
||||||
|
|
||||||
|
//TODO: resolve courses (one for each edition or dictionary)
|
||||||
|
editionForm.Course.Id = 0;
|
||||||
|
|
||||||
|
if (editionForm.Id.HasValue)
|
||||||
|
{
|
||||||
|
var editionToUpdate = await Context.Editions
|
||||||
|
.Include(e => e.AvailableSubjects)
|
||||||
|
.Include(e => e.AvailableInternshipTypes)
|
||||||
|
.FirstOrDefaultAsync(e => e.Id == editionForm.Id.Value, token);
|
||||||
|
|
||||||
|
if (editionToUpdate == null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
editionToUpdate.UpdateEdition(editionForm.EditionStart, editionForm.EditionFinish, editionForm.ReportingStart,
|
||||||
|
editionForm.Course, editionForm.AvailableSubjectsIds, editionForm.AvailableInternshipTypesIds);
|
||||||
|
|
||||||
|
if (!editionToUpdate.IsValidDates)
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var newEdition =
|
||||||
|
Edition.CreateEdition(editionForm.EditionStart.Value, editionForm.EditionFinish.Value, editionForm.ReportingStart.Value,
|
||||||
|
editionForm.Course, editionForm.AvailableSubjectsIds, editionForm.AvailableInternshipTypesIds);
|
||||||
|
|
||||||
|
if (!newEdition.IsValidDates)
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
await Context.Editions.AddAsync(newEdition, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
await Context.SaveChangesAsync(token);
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,23 +4,34 @@ using FluentValidation;
|
|||||||
using InternshipSystem.Core;
|
using InternshipSystem.Core;
|
||||||
using InternshipSystem.Core.Entity.Internship;
|
using InternshipSystem.Core.Entity.Internship;
|
||||||
|
|
||||||
namespace InternshipSystem.Api.Controllers
|
namespace InternshipSystem.Api.Queries
|
||||||
{
|
{
|
||||||
public class EditionForm
|
public class EditionForm
|
||||||
{
|
{
|
||||||
public Guid Id { get; set; }
|
public Guid? Id { get; set; }
|
||||||
public DateTime EditionStart { get; set; }
|
public DateTime? EditionStart { get; set; }
|
||||||
public DateTime EditionFinish { get; set; }
|
public DateTime? EditionFinish { get; set; }
|
||||||
public DateTime ReportingStart { get; set; }
|
public DateTime? ReportingStart { get; set; }
|
||||||
public Course Course { get; set; }
|
public Course Course { get; set; }
|
||||||
public List<InternshipSubject> AvailableSubjects { get; set; }
|
public List<long> AvailableSubjectsIds { get; set; } = new List<long>();
|
||||||
public List<InternshipType> AvailableInternshipTypes { get; set; }
|
public List<long> AvailableInternshipTypesIds { get; set; } = new List<long>();
|
||||||
|
|
||||||
public class Validator : AbstractValidator<EditionForm>
|
public class Validator : AbstractValidator<EditionForm>
|
||||||
{
|
{
|
||||||
public Validator()
|
public Validator()
|
||||||
{
|
{
|
||||||
//TODO: later
|
RuleFor(e => e.Id).NotNull()
|
||||||
|
.When(e => !e.EditionStart.HasValue || !e.EditionFinish.HasValue
|
||||||
|
|| !e.ReportingStart.HasValue || e.Course == null);
|
||||||
|
|
||||||
|
RuleFor(e => e.EditionStart).NotEmpty()
|
||||||
|
.When(e => !e.Id.HasValue);
|
||||||
|
RuleFor(e => e.EditionFinish).NotEmpty()
|
||||||
|
.When(e => !e.Id.HasValue);
|
||||||
|
RuleFor(e => e.ReportingStart).NotEmpty()
|
||||||
|
.When(e => !e.Id.HasValue);
|
||||||
|
RuleFor(e => e.Course).NotNull()
|
||||||
|
.When(e => !e.Id.HasValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ using InternshipSystem.Core.Entity.Internship;
|
|||||||
|
|
||||||
namespace InternshipSystem.Api.Result
|
namespace InternshipSystem.Api.Result
|
||||||
{
|
{
|
||||||
public class EditionFullResult
|
public class EditionDetailsResult
|
||||||
{
|
{
|
||||||
public Guid Id { get; set; }
|
public Guid Id { get; set; }
|
||||||
public DateTime EditionStart { get; set; }
|
public DateTime EditionStart { get; set; }
|
@ -18,17 +18,77 @@ namespace InternshipSystem.Core
|
|||||||
public List<EditionInternshipType> AvailableInternshipTypes { get; set; }
|
public List<EditionInternshipType> AvailableInternshipTypes { get; set; }
|
||||||
|
|
||||||
public bool IsOpen => EditionFinish < DateTime.Today;
|
public bool IsOpen => EditionFinish < DateTime.Today;
|
||||||
|
|
||||||
public Edition CreateEdition(DateTime start, DateTime end, DateTime reportingStart)
|
public static Edition CreateEdition(DateTime start, DateTime end, DateTime reportingStart, Course course,
|
||||||
|
IEnumerable<long> subjectsIds, IEnumerable<long> internshipTypesIds)
|
||||||
|
{
|
||||||
|
var newEdition = CreateEdition(start, end, reportingStart, course);
|
||||||
|
|
||||||
|
newEdition.AvailableSubjects =
|
||||||
|
subjectsIds
|
||||||
|
.Select(s => new EditionSubject
|
||||||
|
{
|
||||||
|
Edition = newEdition,
|
||||||
|
InternshipSubjectId = s,
|
||||||
|
})
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
newEdition.AvailableInternshipTypes =
|
||||||
|
internshipTypesIds
|
||||||
|
.Select(i => new EditionInternshipType
|
||||||
|
{
|
||||||
|
Edition = newEdition,
|
||||||
|
InternshipTypeId = i,
|
||||||
|
})
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
return newEdition;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Edition CreateEdition(DateTime start, DateTime end, DateTime reportingStart, Course course)
|
||||||
{
|
{
|
||||||
return new Edition
|
return new Edition
|
||||||
{
|
{
|
||||||
EditionStart = start,
|
EditionStart = start,
|
||||||
EditionFinish = end,
|
EditionFinish = end,
|
||||||
ReportingStart = reportingStart
|
ReportingStart = reportingStart,
|
||||||
|
Course = course,
|
||||||
|
AvailableSubjects = new List<EditionSubject>(),
|
||||||
|
AvailableInternshipTypes = new List<EditionInternshipType>(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void UpdateEdition(DateTime? start, DateTime? end, DateTime? reportingStart, Course course,
|
||||||
|
IEnumerable<long> subjectsIds, IEnumerable<long> internshipTypesIds)
|
||||||
|
{
|
||||||
|
EditionStart = start ?? EditionStart;
|
||||||
|
EditionFinish = end ?? EditionFinish;
|
||||||
|
ReportingStart = reportingStart ?? ReportingStart;
|
||||||
|
Course = course;
|
||||||
|
|
||||||
|
if (subjectsIds != null)
|
||||||
|
{
|
||||||
|
AvailableSubjects =
|
||||||
|
subjectsIds
|
||||||
|
.Select(s => new EditionSubject
|
||||||
|
{
|
||||||
|
InternshipSubjectId = s,
|
||||||
|
})
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (internshipTypesIds != null)
|
||||||
|
{
|
||||||
|
AvailableInternshipTypes =
|
||||||
|
internshipTypesIds
|
||||||
|
.Select(i => new EditionInternshipType
|
||||||
|
{
|
||||||
|
InternshipTypeId = i,
|
||||||
|
})
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void RegisterInternship(Student student)
|
public void RegisterInternship(Student student)
|
||||||
{
|
{
|
||||||
if (Internships.Any(i => i.Student.Id == student.Id))
|
if (Internships.Any(i => i.Student.Id == student.Id))
|
||||||
@ -55,5 +115,7 @@ namespace InternshipSystem.Core
|
|||||||
{
|
{
|
||||||
return internshipSubjects.All(s => AvailableSubjects.Any(su => su.InternshipSubjectId == s));
|
return internshipSubjects.All(s => AvailableSubjects.Any(su => su.InternshipSubjectId == s));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool IsValidDates => EditionStart <= EditionFinish;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user