// This is the simplest implementation usual in CRUD scenarios // or CRUD scenarios with additional validation. // In complex scenarios creating a new entity and editing existing // once can differ significantly. In that case we recommend to // split this method into two or more. public async Task <ServiceResult <ActionDto> > CreateOrUpdate(ActionDto actionDto) { // Either create a new entity based on the DTO // or change an existing one based on the DTO. Action action = actionDto.RepresentsNewEntity ? actionDto.TranslateTo <Action>() : (await _actionRepository.GetById(actionDto.Id)).CopyPropertiesFrom(actionDto); // Check if the entity exists (if it was an update). if (action == null) { return(EntityNotFound(actionDto)); } // TODO: Later on we will do the checks here. // So far we assume everything always works fine. // Save changes. action = await _actionRepository.AddOrUpdate(action); // If the DTO was representing a new DTO // we need to set the assigned Id. // If it already had the Id, it is the same one. // So we can simply have an assignment here. actionDto.Id = action.Id; // Remove ActionsAll from cache, next time getall call will fill the cache. CacheHelper cacheHelper = new CacheHelper(_memoryCache); cacheHelper.RemoveActions(); return(Ok(actionDto)); }
public async Task <ServiceResult <ProjectDto> > CreateOrUpdate(ProjectDto projectDto) { Project project = projectDto.RepresentsNewEntity ? projectDto.TranslateTo <Project>() : (await _projectRepository.GetById(projectDto.Id)).CopyPropertiesFrom(projectDto); if (project == null) { return(EntityNotFound(projectDto)); } // TODO: Later on we will do the checks here. // So far we assume everything always works fine. project = await _projectRepository.AddOrUpdate(project); projectDto.Id = project.Id; return(Ok(projectDto)); }
public async Task <ServiceResult <ActionListDto> > CreateOrUpdate(ActionListDto actionListDto) { ActionList list = actionListDto.RepresentsNewEntity ? actionListDto.TranslateTo <ActionList>() : (await _listRepository.GetById(actionListDto.Id)).CopyPropertiesFrom(actionListDto); if (list == null) { return(EntityNotFound(actionListDto)); } // TODO: Later on we will do the checks here. // So far we assume everything always works fine. list = await _listRepository.AddOrUpdate(list); actionListDto.Id = list.Id; return(Ok(actionListDto)); }