public async Task <bool> AddNewEmailList(RequestEmailList model) { using (var transaction = _database.Database.BeginTransaction()) { try { var original = new EmailList(); AutoMapper.Mapper.Map(model, original); if (!ReferenceEquals(model.Guids, null)) { foreach (Guid modelGuid in model.Guids) { original.Contacts.Add(await _database.Contacts.FirstOrDefaultAsync(p => p.GuID == modelGuid)); } } _database.EmailLists.Add(original); await _database.SaveChangesAsync(); transaction.Commit(); return(true); } catch { transaction.Rollback(); throw; } } }
public async Task <bool> DeleteContactsFromEmailListAsync(RequestEmailList model) { var emails = _database.EmailLists.Include(p => p.Contacts); var email = await emails.FirstOrDefaultAsync(p => p.EmailListID == model.EmailListID); if (!email.Contacts.Any()) { return(false); } var list = new List <Contact>(); model.Guids.ForEach(p => list.Add(email.Contacts.SingleOrDefault(z => z.GuID == p))); using (var transaction = _database.Database.BeginTransaction()) { try { this._database.Configuration.LazyLoadingEnabled = false; list.ForEach(p => email.Contacts.Remove(p)); _database.Entry(email).State = EntityState.Modified; await _database.SaveChangesAsync(); transaction.Commit(); return(true); } catch { transaction.Rollback(); throw; } } }
public async Task <bool> AddContactsToExsistingList(RequestEmailList model) { using (var transaction = _database.Database.BeginTransaction()) { try { var original = await _database.EmailLists.FirstOrDefaultAsync(p => model.EmailListID == p.EmailListID); var list = new List <Contact>(); foreach (Guid modelGuid in model.Guids) { var item = await _database.Contacts.FirstOrDefaultAsync(z => z.GuID == modelGuid); list.Add(item); } original.Contacts.ForEach(z => { if (list.Contains(z)) { list.Remove(z); } }); list.ForEach(p => original.Contacts.Add(p)); _database.Entry(original).State = EntityState.Modified; await _database.SaveChangesAsync(); transaction.Commit(); return(true); } catch { transaction.Rollback(); throw; } } }
public async Task <HttpResponseMessage> DeleteEmailListContactsAsync([FromBody] RequestEmailList model) { if (await manager.DeleteContactsFromEmailListAsync(model)) { return(Request.CreateResponse(HttpStatusCode.Accepted)); } return(Request.CreateResponse(HttpStatusCode.NotModified)); }
public async Task <HttpResponseMessage> PutAddToExsistingEmailListAsync([FromBody] RequestEmailList model) { if (await manager.AddContactsToExsistingList(model)) { return(Request.CreateResponse(HttpStatusCode.OK)); } return(Request.CreateResponse(HttpStatusCode.BadRequest)); }
public async Task <HttpResponseMessage> PutEmailListAsync([FromBody] RequestEmailList model) { if (await manager.UpdateEmailListAsync(model)) { return(Request.CreateResponse(HttpStatusCode.OK)); } return(Request.CreateResponse(HttpStatusCode.NotModified)); }
public async Task <HttpResponseMessage> PostEmailListAsync([FromBody] RequestEmailList model) { if (await manager.AddNewEmailList(model)) { return(Request.CreateResponse(HttpStatusCode.Created)); } return(Request.CreateResponse(HttpStatusCode.BadRequest)); }
public async Task <IHttpActionResult> PostCreateEmailList([FromBody] RequestEmailList emailList) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } EmailList newEmailList = await appManager.AddNewEmailListAsync(emailList); return(Created($"Contacts?id = {newEmailList.EmailListID}", new ViewEmailList(newEmailList))); }
public async Task <IHttpActionResult> PutUpdateEmailListFull([FromBody] RequestEmailList emailList) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } EmailList emailListToUpdate = await appManager.GetEmailListByIdAsync(emailList.EmailListID); if (emailListToUpdate == null) { return(NotFound()); } EmailList updatedEmailList = await appManager.ModifyEmailListAsync(emailListToUpdate, emailList.Contacts, emailList.EmailListName, true); if (updatedEmailList != null) { return(Ok(new ViewEmailList(updatedEmailList))); } return(BadRequest()); }
public async Task <bool> UpdateEmailListAsync(RequestEmailList model) { using (var transaction = _database.Database.BeginTransaction()) { try { var original = await _database.EmailLists.Include(p => p.Contacts) .FirstOrDefaultAsync(p => p.EmailListID == model.EmailListID); if (!ReferenceEquals(model.EmailListName, null)) { original.EmailListName = model.EmailListName; } if (!ReferenceEquals(model.Guids, null)) { var contacts = new List <Contact>(); original.Contacts.Clear(); foreach (Guid emaillistGuid in model.Guids) { contacts.Add(await _database.Contacts.FirstOrDefaultAsync(p => p.GuID == emaillistGuid)); } original.Contacts = contacts; } _database.Entry(original).State = EntityState.Modified; await _database.SaveChangesAsync(); transaction.Commit(); return(true); } catch { transaction.Rollback(); throw; } } }
public async Task <EmailList> AddNewEmailListAsync(RequestEmailList requestEmailList) { EmailList newEmailList = new EmailList(); using (DbContextTransaction transaction = db.Database.BeginTransaction()) { newEmailList.EmailListName = requestEmailList.EmailListName; if (requestEmailList.Contacts != null) { foreach (Guid guid in requestEmailList.Contacts) { var cont = await db.Contacts.FirstOrDefaultAsync(x => x.GuID == guid); if (cont != null) { newEmailList.Contacts.Add(cont); } } } try { db.EmailLists.Add(newEmailList); await db.SaveChangesAsync(); transaction.Commit(); return(newEmailList); } catch { transaction.Rollback(); throw; } } }