public async Task saveClientChapter(ChapterClientDto payload) { ChapterClient cc = null; if (payload.Id != null) { cc = await _context.ChapterClient.FirstOrDefaultAsync(cc => cc.Id == payload.Id); clientChapterBuilder(ref payload, ref cc); _context.ChapterClient.Update(cc); } else { cc = new ChapterClient(); clientChapterBuilder(ref payload, ref cc); await _context.ChapterClient.AddAsync(cc); } if (payload.Primary == true) { var removeFromPrimary = await _context.ChapterClient.Where(ch => ch.ClientId == cc.ClientId && cc.Id != ch.Id).ToListAsync(); removeFromPrimary.ForEach(el => { el.Primary = false; }); _context.ChapterClient.UpdateRange(removeFromPrimary); } await _context.SaveChangesAsync(); }
private static void clientChapterBuilder(ref ChapterClientDto payload, ref ChapterClient cc) { cc.RegistrationDate = payload.RegistrationDate; cc.ChapterId = payload.ChapterId.GetValueOrDefault(); cc.ClientId = payload.ClientId.GetValueOrDefault(); cc.Primary = payload.Primary; cc.NewRegistration = payload.NewRegistration; cc.UpdatedAt = DateTime.Now; }
public async Task <IActionResult> SaveChaoterClient(ChapterClientDto payload) { try { await _service.saveClientChapter(payload); return(Ok()); } catch (Exception ex) { return(DefaultError(ex)); } }