public IHttpActionResult GuardarPeriodosNoLectivos([FromUri] int id, [FromBody] PeriodoNoLectivosSaveParameters model) { model.AccountId = id; var result = _canvasExtend.GuardarPeriodosNoLectivos(model); if (!result.HasErrors) { return(Ok(result.Value)); } return(ResultWithMessages(result)); }
public ResultValue <AccountGenerateDto> GuardarPeriodosNoLectivos(PeriodoNoLectivosSaveParameters model) { var result = new ResultValue <AccountGenerateDto>(); var resValid = VerificarPeriodosNoLectivos(model); if (resValid.HasErrors) { result.Errors.AddRange(resValid.Errors); return(result); } var accountGenerate = _context.AccountGenerates.Find(model.AccountId); var periodosNoLectivos = accountGenerate.PeriodosNoLectivos.ToList(); var ids = model.Fechas.Where(f => f.Id.HasValue).Select(f => f.Id.Value).ToArray(); var eliminados = periodosNoLectivos.Where(p => !ids.Contains(p.Id)).ToList(); foreach (var periodoNoLectivo in eliminados) { _context.PeriodosNoLectivos.Remove(periodoNoLectivo); } foreach (var rangoFecha in model.Fechas) { var periodoNoLectivo = accountGenerate.PeriodosNoLectivos.FirstOrDefault(p => p.Id == rangoFecha.Id); if (periodoNoLectivo == null) { periodoNoLectivo = new PeriodoNoLectivo { Inicio = rangoFecha.Inicio, Fin = rangoFecha.Fin }; } else { periodoNoLectivo.Inicio = rangoFecha.Inicio; periodoNoLectivo.Fin = rangoFecha.Fin; } accountGenerate.PeriodosNoLectivos.Add(periodoNoLectivo); } _context.SaveChanges(); result.Value = new AccountGenerateDto { Id = accountGenerate.Id, Name = accountGenerate.Name, PeriodosNoLectivos = accountGenerate.PeriodosNoLectivos.Select(p => new PeriodoNoLectivoDto { Id = p.Id, Inicio = p.Inicio, Fin = p.Fin }) }; return(result); }
public ResultValue <bool> VerificarPeriodosNoLectivos(PeriodoNoLectivosSaveParameters model) { var result = new ResultValue <bool>(); var accountPeriodo = _context.AccountGenerates.Find(model.AccountId); if (accountPeriodo == null) { result.Errors.Add(string.Format(CanvasExtedStrings.ErrorNoExisteAccountExtend, model.AccountId)); return(result); } var resCourses = _accountsApi.GetCourses(model.AccountId); if (resCourses.HasErrors) { result.Errors.AddRange(resCourses.Errors); return(result); } var isCoursePublish = resCourses.Elements.Any(c => c.GetState() == CourseState.Available); if (isCoursePublish) { result.Errors.Add(CanvasExtedStrings.ErrorCoursePublish); return(result); } var isFechaFueraPeriodo = model.Fechas.Any(rango => { if (rango.Inicio > accountPeriodo.FechaFin || rango.Inicio < accountPeriodo.FechaInicio) { return(true); } if (rango.Fin < accountPeriodo.FechaInicio || rango.Fin > accountPeriodo.FechaFin) { return(true); } return(false); }); var newId = -1; foreach (var rangoFecha in model.Fechas) { rangoFecha.Id = --newId; } var sobrePuesto = model.Fechas.Any(rango1 => { var primero = model.Fechas.First(f => f.Inicio == rango1.Inicio && f.Fin == rango1.Fin); var rangosVerify = model.Fechas.Where(f => f.Id != primero.Id).ToList(); var solapado = rangosVerify.Any(rango2 => Solapado(rango1.Inicio, rango1.Fin, rango2.Inicio, rango2.Fin)); return(solapado); }); result.Value = !sobrePuesto && !isFechaFueraPeriodo; if (isFechaFueraPeriodo) { result.Errors.Add(CanvasExtedStrings.ErrorFechasNoPeriodoActivo); } if (sobrePuesto) { result.Errors.Add(CanvasExtedStrings.ErrorFechasSobrePuestas); } return(result); }