private ActionResponse ValidateDrill(DrillDTO drill, bool isUpdate = false) { DrillsRepository repository = RepositoriesFactory.CreateRepository <DrillsRepository, Drill>(); MuscleGroupsRepository muscleGroupRep = RepositoriesFactory.CreateRepository <MuscleGroupsRepository, MuscleGroup>(); if (repository.Query().Any(currDrill => currDrill.DrillId != drill.Id && currDrill.Name == drill.Name)) { return(new ActionResponse() { CompletedSuccessfully = false, ErrorMessage = "A drill with the given name already exists" }); } bool doAllMuscleGroupsExist = drill.MuscleGroups.All(currNewMuscleGroup => { return(muscleGroupRep.Query().Any(CurrMuscleGroup => CurrMuscleGroup.MuscleGroupId == currNewMuscleGroup.Id)); }); if (!doAllMuscleGroupsExist) { return(new ActionResponse() { CompletedSuccessfully = false, ErrorMessage = "None existing muscle group" }); } return(null); }
public ActionResponse DeleteDrill(int drillId) { ActionResponse response; response = CanUserEditDrill(drillId); if (response != null) { return(response); } DrillsRepository drillRepository = RepositoriesFactory.CreateRepository <DrillsRepository, Drill>(); drillRepository.Delete(drillId); return(new ActionResponse() { CompletedSuccessfully = true }); }
public ActionResponse UpdateDrill(DrillDTO newDrill) { ActionResponse response; response = ValidateDrill(newDrill); if (response != null) { return(response); } response = CanUserEditDrill(newDrill.Id); if (response != null) { return(response); } Drill drillForUpdate = ConvertDTOToEntity(newDrill); drillForUpdate.CreatorUserId = GetCurrentUser().UserId; DrillsRepository drillRepository = RepositoriesFactory.CreateRepository <DrillsRepository, Drill>(); Drill existingDrill = drillRepository.Get(drillForUpdate.DrillId); existingDrill.Name = drillForUpdate.Name; existingDrill.Description = drillForUpdate.Description; existingDrill.DrillMuscleGroups = drillForUpdate.DrillMuscleGroups; drillRepository.Update(existingDrill); return(new ActionResponse() { CompletedSuccessfully = true }); }