public async Task<int> CreateAsync(FailureReport report) { if (report == null) throw new ArgumentNullException(); _dbContext.Users.Attach(report.Reporter); report.Items.ToList().ForEach(ft => _dbContext.FailureTypes.Attach(ft.Type)); _dbContext.FailureReports.Add(report); await _dbContext.SaveChangesAsync(); return report.Id; }
public async Task SaveAsync(FailureReport report) { if (report.Feedback != null) if (report.FeedbackId == null) _dbContext.Feedbacks.Add(report.Feedback); else { report.Feedback.Id = report.FeedbackId.Value; _dbContext.Feedbacks.Attach(report.Feedback); _dbContext.Entry(report.Feedback).State = EntityState.Modified; await _dbContext.SaveChangesAsync(); } _dbContext.FailureReports.Attach(report); _dbContext.Entry(report).State = EntityState.Modified; await _dbContext.SaveChangesAsync(); }
/// <summary> /// Check the failure types and add them to a list. /// </summary> /// <param name="failureReport">The report contains the types.</param> /// <param name="itemsToAdd">Failure types to add.</param> /// <returns>A value indicates whether the operation is succeed.</returns> private bool CheckTypesAndAdd(FailureReport failureReport, ICollection<FailureItemDto> itemsToAdd) { if (failureReport == null || itemsToAdd == null) throw new ArgumentNullException(); //get all enabled types var failureTypes = _typeRepo.GetAllTypes().ToList(); if (IsDuplicateOrInvalid(itemsToAdd, failureTypes)) return false; failureReport.Items = itemsToAdd.Select(item => new FailureItem { Count = item.Count, Detail = item.Detail, Report = failureReport, Type = failureTypes.First(ft => ft.Id == item.TypeId) }).ToList(); return InSameCategory(failureReport); }
private static bool InSameCategory(FailureReport failureReport) { return failureReport.Items.GroupBy(item => item.Type.CategoryId).Count() == 1; }
public async Task<IHttpActionResult> Post(FailureReportCreationDto dto) { var user = await User.GetEntityAsync(_identityRepo); if (IsDuplicateSubmit(dto, user)) { return StatusCode(HttpStatusCode.Created); } var report = new FailureReport { Title = dto.Title, Time = DateTime.UtcNow, State = ReportStates.Waiting, Items = new List<FailureItem>() }; if (!CheckTypesAndAdd(report, dto.Items)) return BadRequest("故障项不正确"); report.Reporter = user; await _failureRepo.CreateAsync(report); return StatusCode(HttpStatusCode.Created); }