/// <summary> /// Creates a new data transfer object for a given week. /// </summary> /// <param name="week">The week to create a DTO for.</param> public WeekBaseDTO(WeekBase week) { this.Name = week.Name; try { this.Thumbnail = new WeekPictogramDTO(week.Thumbnail); } catch (NullReferenceException) { this.Thumbnail = new WeekPictogramDTO(); } Days = new List <WeekdayDTO>(); foreach (var day in week.Weekdays.OrderBy(wd => wd.Day)) { Days.Add(new WeekdayDTO(day)); } }
/// <summary> /// From the given DTO, set the name, thumbnail and days of the given week object. /// </summary> /// <param name="weekDTO">The DTO from which values are read.</param> /// <param name="week">The week object to which values are written.</param> /// <param name="_giraf">An instance of the GirafService from which the database will be accessed when reading the DTO.</param> /// <returns>MissingProperties if thumbnail is missing. /// ResourceNotFound if any pictogram id is invalid. /// null otherwise.</returns> public static async Task <ErrorResponse> SetWeekFromDTO(WeekBaseDTO weekDTO, WeekBase week, IGirafService _giraf) { var modelErrorCode = weekDTO.ValidateModel(); if (modelErrorCode.HasValue) { return(new ErrorResponse(modelErrorCode.Value)); } week.Name = weekDTO.Name; Pictogram thumbnail = _giraf._context.Pictograms .FirstOrDefault(p => p.Id == weekDTO.Thumbnail.Id); if (thumbnail == null) { return(new ErrorResponse(ErrorCode.MissingProperties, "thumbnail")); } week.Thumbnail = thumbnail; foreach (var day in weekDTO.Days) { var wkDay = new Weekday(day); if (!(await AddPictogramsToWeekday(wkDay, day, _giraf))) { return(new ErrorResponse(ErrorCode.ResourceNotFound, "pictogram")); } week.UpdateDay(wkDay); } //All week days that were not specified in the new schedule, but existed before var toBeDeleted = week.Weekdays.Where(wd => !weekDTO.Days.Any(d => d.Day == wd.Day)).ToList(); foreach (var deletedDay in toBeDeleted) { week.Weekdays.Remove(deletedDay); } return(null); }