public async Task <ActionResponse <PlanDayDto> > AddDayToPlan(PlanDayDto entityDto) { try { List <PlanDaySubjectDto> planDaySubjects = new List <PlanDaySubjectDto>(entityDto.PlanDaySubjects); entityDto.PlanDaySubjectIds = null; entityDto.PlanDaySubjects = null; var entityToAdd = mapper.Map <PlanDayDto, PlanDay>(entityDto); unitOfWork.GetGenericRepository <PlanDay>().Add(entityToAdd); unitOfWork.Save(); mapper.Map(entityToAdd, entityDto); entityDto.PlanDaySubjects = planDaySubjects.Select(pd => { pd.PlanDayId = entityDto.Id; return(pd); }).ToList(); if ((await ModifyPlanDaySubjects(entityDto)).IsNotSuccess(out ActionResponse <PlanDayDto> response, out entityDto)) { return(response); } return(await ActionResponse <PlanDayDto> .ReturnSuccess(mapper.Map(entityToAdd, entityDto), "Dan plana uspješno unesen.")); } catch (Exception) { return(await ActionResponse <PlanDayDto> .ReturnError("Greška prilikom dodavanja dana u plan.")); } }
public async Task <ActionResponse <PlanDayDto> > UpdatePlanDay(PlanDayDto entityDto) { try { List <PlanDaySubjectDto> planDaySubjects = new List <PlanDaySubjectDto>(entityDto.PlanDaySubjects); entityDto.PlanDaySubjectIds = null; entityDto.PlanDaySubjects = null; var entityToUpdate = unitOfWork.GetGenericRepository <PlanDay>().FindSingle(entityDto.Id.Value); mapper.Map(entityDto, entityToUpdate); unitOfWork.GetGenericRepository <PlanDay>().Update(entityToUpdate); unitOfWork.Save(); entityDto.PlanDaySubjects = planDaySubjects.Select(pd => { pd.PlanDayId = entityDto.Id; return(pd); }).ToList(); if ((await ModifyPlanDaySubjects(entityDto)).IsNotSuccess(out ActionResponse <PlanDayDto> response, out entityDto)) { return(response); } return(await ActionResponse <PlanDayDto> .ReturnSuccess(mapper.Map(entityToUpdate, entityDto))); } catch (Exception) { return(await ActionResponse <PlanDayDto> .ReturnError("Greška prilikom ažuriranja dana plana.")); } }
public async Task <ActionResponse <PlanDayDto> > RemoveDayFromPlan(PlanDayDto planDay) { try { unitOfWork.GetGenericRepository <PlanDay>().Delete(planDay.Id.Value); unitOfWork.Save(); return(await ActionResponse <PlanDayDto> .ReturnSuccess(null, "Dan uspješno izbrisan iz plana.")); } catch (Exception) { return(await ActionResponse <PlanDayDto> .ReturnError("Greška prilikom micanja dana iz plana.")); } }
public async Task <ActionResponse <PlanDayDto> > ModifyPlanDaySubjects(PlanDayDto entityDto) { var entity = unitOfWork.GetGenericRepository <PlanDay>() .FindBy(p => p.Id == entityDto.Id, includeProperties: "Subjects.PlanDaySubjectThemes.Theme"); entityDto.PlanDaySubjectIds = null; var currentSubjects = mapper.Map <List <PlanDaySubject>, List <PlanDaySubjectDto> >(entity.Subjects.ToList()); var subjectsToRemove = currentSubjects.Where(cd => !entityDto.PlanDaySubjects .Select(pds => pds.SubjectId.Value) .Contains(cd.SubjectId.Value)) .ToList(); var subjectsToAdd = entityDto.PlanDaySubjects .Where(nt => !currentSubjects.Select(cd => cd.SubjectId.Value).Contains(nt.SubjectId.Value)) .Select(pds => new PlanDaySubjectDto { PlanDayId = entity.Id, SubjectId = pds.SubjectId, PlanDaySubjectThemes = pds.PlanDaySubjectThemes }) .ToList(); var subjectsToModify = entityDto.PlanDaySubjects.Where(cd => currentSubjects .Select(pds => pds.SubjectId.Value) .Contains(cd.SubjectId.Value)) .ToList(); if ((await RemoveSubjectsFromPlanDay(subjectsToRemove)) .IsNotSuccess(out ActionResponse <List <PlanDaySubjectDto> > actionResponse)) { return(await ActionResponse <PlanDayDto> .ReturnError("Neuspješno ažuriranje predmeta u danu plana.")); } if ((await AddSubjectsToPlanDay(subjectsToAdd)).IsNotSuccess(out ActionResponse <List <PlanDaySubjectDto> > subjectsResponse, out subjectsToAdd)) { return(await ActionResponse <PlanDayDto> .ReturnError("Neuspješno ažuriranje predmeta u danu plana.")); } if ((await ModifySubjectsInPlanDay(subjectsToModify)).IsNotSuccess(out actionResponse)) { return(await ActionResponse <PlanDayDto> .ReturnError("Neuspješno ažuriranje predmeta u danu plana.")); } return(await ActionResponse <PlanDayDto> .ReturnSuccess(entityDto, "Uspješno izmijenjeni dani plana.")); }