diff --git a/Server/Controllers/CourseController.cs b/Server/Controllers/CourseController.cs index 208a75fdd76a018d925fdbbdf8009cca2aa27452..5d5619b7eda04c07441e37286d3e871900e19a82 100644 --- a/Server/Controllers/CourseController.cs +++ b/Server/Controllers/CourseController.cs @@ -92,7 +92,7 @@ namespace FPH.EBL_Dienstplan.Server.Controllers [HttpGet] [Route("{Id}/dutyshifts")] - public ActionResult<IEnumerable<IUser>> GetDutyShiftsByCourseId(Guid Id) + public ActionResult<IEnumerable<IDutyShift>> GetDutyShiftsByCourseId(Guid Id) { Course? course = dbContext.Courses.Include(x => x.DutyShifts).FirstOrDefault(x => x.Id == Id); if (course is null) return NotFound(); diff --git a/Server/Controllers/DutyShiftController.cs b/Server/Controllers/DutyShiftController.cs index 96f0038af0db566c7e9d659057a522b3eb2ead85..b9af4c67ba65150e97ca8fd10df6cc66e6f0c562 100644 --- a/Server/Controllers/DutyShiftController.cs +++ b/Server/Controllers/DutyShiftController.cs @@ -20,22 +20,24 @@ namespace FPH.EBL_Dienstplan.Server.Controllers [Authorize(Policy = IsInternalTutorRequirement.Policy)] [Authorize(Policy = IsExternalTutorRequirement.Policy)] [Route("")] - public ActionResult<IEnumerable<IDutyShiftWithPlaces>> GetAllDutyShifts(bool showElapsed = false) + public ActionResult<IEnumerable<IDutyShiftWithPlacesAndCourse>> GetAllDutyShifts(bool showElapsed = false) { if (UserJwtToken is null) return Unauthorized(); bool showInternal = UserJwtToken.isInternalTutor; bool showExternal = UserJwtToken.isExternalTutor; - return Ok(dbContext.DutyShifts + IQueryable<DutyShift> data = dbContext.DutyShifts .Include(ds => ds.DutyShiftPlaces).ThenInclude(dsp => dsp.Place) .Include(ds => ds.DutyShiftPlaces).ThenInclude(dsp => dsp.Tutor1) .Include(ds => ds.DutyShiftPlaces).ThenInclude(dsp => dsp.Tutor2) - .Where(ds => showElapsed || ds.EndTime > DateTime.Now).Where(ds => ds.IsExternal && showExternal || !ds.IsExternal && showInternal)); + .Include(ds => ds.Course) + .Where(ds => showElapsed || ds.EndTime > DateTime.Now).Where(ds => ds.IsExternal && showExternal || !ds.IsExternal && showInternal); + return Ok(data); } [HttpPut] [Authorize(Policy = IsAdminRequirement.Policy)] [Route("")] - public async Task<ActionResult<IDutyShift>> AddDutyShift(DateTime starttime, DateTime endtime, Guid courseId, Guid dutyShiftCategoryId, bool canhavetwotutors, string comment) + public async Task<ActionResult<IDutyShift>> AddDutyShift(DateTime starttime, DateTime endtime, Guid courseId, Guid dutyShiftCategoryId, bool canhavetwotutors, bool isExternal, string? comment) { Course? course = dbContext.Courses.FirstOrDefault(x => x.Id == courseId); if (course is null) return NotFound(); @@ -48,7 +50,8 @@ namespace FPH.EBL_Dienstplan.Server.Controllers CourseId = course.Id, DutyShiftCategoryId = dutyShiftCategory.Id, CanHaveTwoTutors = canhavetwotutors, - Comment = comment + Comment = comment ?? "", + IsExternal = isExternal }; dbContext.DutyShifts.Add(dutyShift); @@ -71,12 +74,13 @@ namespace FPH.EBL_Dienstplan.Server.Controllers [HttpGet] [Route("{Id}")] - public ActionResult<IDutyShiftWithPlaces> GetDutyShiftById(Guid Id) + public ActionResult<IDutyShiftWithPlacesAndCourse> GetDutyShiftById(Guid Id) { - DutyShift? dutyShift = dbContext.DutyShifts + IDutyShiftWithPlacesAndCourse? dutyShift = (IDutyShiftWithPlacesAndCourse?)dbContext.DutyShifts .Include(ds => ds.DutyShiftPlaces).ThenInclude(dsp => dsp.Place) .Include(ds => ds.DutyShiftPlaces).ThenInclude(dsp => dsp.Tutor1) .Include(ds => ds.DutyShiftPlaces).ThenInclude(dsp => dsp.Tutor2) + .Include(ds => ds.Course) .FirstOrDefault(x => x.Id == Id); if (dutyShift is null) return NotFound(); return Ok(dutyShift); @@ -158,6 +162,18 @@ namespace FPH.EBL_Dienstplan.Server.Controllers return Ok(dutyShift); } + [HttpPatch] + [Authorize(Policy = IsAdminRequirement.Policy)] + [Route("{Id}/isexternal")] + public async Task<ActionResult<IDutyShift>> UpdateDutyShiftIsExternal(Guid Id, bool isexternal) + { + DutyShift? dutyShift = dbContext.DutyShifts.FirstOrDefault(x => x.Id == Id); + if (dutyShift is null) return NotFound(); + dutyShift.IsExternal = isexternal; + await dbContext.SaveChangesAsync(); + return Ok(dutyShift); + } + [HttpGet] [Authorize(Policy = IsAdminRequirement.Policy)] [Authorize(Policy = IsInternalTutorRequirement.Policy)] diff --git a/Server/Models/Course.cs b/Server/Models/Course.cs index 1e6548f7826e0f067e619b3eb9af643fa56a205f..be8f156be987f4d83596e836381a979a561ebfa7 100644 --- a/Server/Models/Course.cs +++ b/Server/Models/Course.cs @@ -1,5 +1,6 @@ using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.Text.Json.Serialization; namespace FPH.EBL_Dienstplan.Server.Models { diff --git a/Server/Models/DutyShift.cs b/Server/Models/DutyShift.cs index 1197e506e54cd95a343790427c868cc5b796e3c1..c6a5cb8d7ff6ceea68f6add7dde367e8425a2008 100644 --- a/Server/Models/DutyShift.cs +++ b/Server/Models/DutyShift.cs @@ -28,8 +28,9 @@ namespace FPH.EBL_Dienstplan.Server.Models bool IsExternal { get; set; } string Comment { get; set; } } - public interface IDutyShiftWithPlaces : IDutyShift + public interface IDutyShiftWithPlacesAndCourse : IDutyShift { IEnumerable<IDutyShiftPlaceWithPlaceAndTutors> DutyShiftPlaces { get; set; } + ICourse? Course { get; set; } } } diff --git a/Server/Models/DutyShiftPlace.cs b/Server/Models/DutyShiftPlace.cs index ad73a8f23196264882c54d41fecb3f18234e6129..b31734f4e2daa5cd37a98328a2b1047572326ca7 100644 --- a/Server/Models/DutyShiftPlace.cs +++ b/Server/Models/DutyShiftPlace.cs @@ -11,9 +11,9 @@ namespace FPH.EBL_Dienstplan.Server.Models [ForeignKey(nameof(DutyShiftId))] public virtual DutyShift? DutyShift { get; set; } [Required] public required Guid PlaceId { get; set; } [ForeignKey(nameof(PlaceId))] public virtual Place? Place { get; set; } - public Guid TutorId1 { get; set; } + public Guid? TutorId1 { get; set; } [ForeignKey(nameof(TutorId1))] public virtual User? Tutor1 { get; set; } - public Guid TutorId2 { get; set; } + public Guid? TutorId2 { get; set; } [ForeignKey(nameof(TutorId2))] public virtual User? Tutor2 { get; set; } [InverseProperty(nameof(DutyShiftPlaceStudent.DutyShiftPlace))] public virtual ICollection<DutyShiftPlaceStudent> DutyShiftPlaceStudents { get; set; } = []; } @@ -22,13 +22,13 @@ namespace FPH.EBL_Dienstplan.Server.Models Guid Id { get; set; } Guid DutyShiftId { get; set; } Guid PlaceId { get; set; } - Guid TutorId1 { get; set; } - Guid TutorId2 { get; set; } + Guid? TutorId1 { get; set; } + Guid? TutorId2 { get; set; } } public interface IDutyShiftPlaceWithPlaceAndTutors : IDutyShiftPlace { IPlace Place { get; set; } - IUser Tutor1 { get; set; } - IUser Tutor2 { get; set; } + IUser? Tutor1 { get; set; } + IUser? Tutor2 { get; set; } } } diff --git a/Server/Program.cs b/Server/Program.cs index 5066ee4b6d34b9f2780b7fe0b96613393249de39..4374932af903143bed0eee316a5f6c82619b5dd5 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -10,6 +10,7 @@ using FPH.EBL_Dienstplan.Server.Authorization; using Microsoft.Net.Http.Headers; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authentication; +using System.Text.Json.Serialization; namespace FPH.EBL_Dienstplan.Server { @@ -77,7 +78,7 @@ namespace FPH.EBL_Dienstplan.Server //Services - builder.Services.AddControllers(); + builder.Services.AddControllers().AddJsonOptions(x=> x.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen();