/// <summary> /// Adds new project to db if Id is 0 or updates existing project. /// </summary> /// <param name="projectViewModel"></param> public void AddOrUpdate(ProjectViewModel projectViewModel) { // Get project entity from view model var project = _mapper.Map <Project>(projectViewModel); if (projectViewModel.Id == 0) { _projectRepo.Add(project); } else { _projectRepo.Update(project); } }
public void UpdateAll(List <Project> projectsFetched) { var projects = _repository.GetAll(); // Delete should be cascade if it's necessary // foreach (var project in projects) // { // if (!projectsFetched.Select(v => v.Id).Contains(project.Id)) // _repository.Delete(project); // } foreach (var projectFetched in projectsFetched.Where(v => v.Name != null && v.BaseSeries != null)) { var foundProject = projects.SingleOrDefault(v => v.Id == projectFetched.Id); if (foundProject == null) { var uniqueKeyViolation = _repository.GetByUniqueKey(projectFetched.BaseSeries); if (uniqueKeyViolation == null) { _repository.Add(projectFetched); } } else { var wasChanged = false; var name = projectFetched.Name.Trim(); if (foundProject.Name != name) { foundProject.Name = name; wasChanged = true; } var baseSeries = projectFetched.BaseSeries.Trim(); if (foundProject.Name != name) { foundProject.Name = name; wasChanged = true; } if (wasChanged) { _repository.Update(foundProject); } } } }
public async Task <bool> AddProject(int userId, ProjectDto projectDto) { // TODO: [TESTS] (ProjectService.AddProject) Add tests // TODO: [VALIDATION] (ProjectService.AddProject) Add validation var builder = new ServiceMetricBuilder(nameof(ProjectService), nameof(AddProject)) .WithCategory(MetricCategory.Project, MetricSubCategory.Add) .WithCustomInt1(userId) .WithCustomInt2(projectDto.ProductId) .WithCustomInt3(0); try { using (builder.WithTiming()) { var projectEntity = projectDto.AsProjectEntity(); projectEntity.UserId = userId; using (builder.WithCustomTiming1()) { builder.IncrementQueryCount(); if (await _projectRepo.Add(projectEntity) <= 0) { return(false); } builder.WithResultsCount(1); return(true); } } } catch (Exception ex) { _logger.LogUnexpectedException(ex); builder.WithException(ex); return(false); } finally { await _metrics.SubmitPointAsync(builder.Build()); } }