public ActionResult Create([Bind(Include = "ContactId,FirstName,LastName,Email,Mobile,Landline,Notes")] Contact contact) { if (ModelState.IsValid) { db.Contacts.Add(contact); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(contact)); }
public string SaveEditContact(EditModalView viewModel) { string contactCheck = ""; ContactDetail cd = _context.ContactDetails.Where(x => x.Id == viewModel.ContactID).Distinct().FirstOrDefault(); if (cd != null) { cd = new ContactDetail { Id = viewModel.ContactID, ContactName = viewModel.ContactName, ContactSurname = viewModel.ContactSurname, ContactEmail = viewModel.ContactEmail, DateCreated = DateTime.Now }; _context.ContactDetails.AddOrUpdate(cd); _context.SaveChanges(); } else { contactCheck = "There is no such !"; } return(contactCheck); }
public string AddContact(string contactName, string contactSurname, string contactEmail) { string contactCheck = ""; ContactDetail cd = _context.ContactDetails.Where(x => x.ContactName == contactSurname || x.ContactEmail == contactEmail).Distinct().FirstOrDefault(); if (cd != null) { contactCheck = cd.ContactName.ToString() + "Already exists !"; } else { cd = new ContactDetail { Id = Guid.NewGuid(), ContactName = contactName, ContactSurname = contactSurname, ContactEmail = contactEmail, DateCreated = DateTime.Now }; _context.ContactDetails.AddOrUpdate(cd); _context.SaveChanges(); contactCheck = cd.ContactName.ToString() + "Added Successfully"; } return(contactCheck); }
public void UpdatePerson(IPerson person) { var ctx = new AddressBookContext(); var entity = ctx.Persons.Find(person.ID); Mappings.Map(person, entity); ctx.SaveChanges(); }
public void DeletePerson(int personID) { var ctx = new AddressBookContext(); var person = ctx.Persons.Find(personID); ctx.Persons.Remove(person); ctx.SaveChanges(); }
public void InsertPerson(IPerson person) { var ctx = new AddressBookContext(); var entity = new Person(); Mappings.Map(person, entity); ctx.Persons.Add(entity); ctx.SaveChanges(); }
public void DeleteFriend(Friend deleteFriend) { using (AddressBookContext dataContext = new AddressBookContext()) { Friend friendToDelete = dataContext.Friends.Single(t => t.ID == deleteFriend.FirstName); dataContext.Friends.Remove(friendToDelete); dataContext.SaveChanges(); } }
public IActionResult Add(Contacts contact) { try { db.Contacts.Add(contact); // сохраняем в бд все изменения db.SaveChanges(); Contacts last = db.Contacts.Last(); ViewBag.Message = "Контакт с ID " + last.Id + " успешно добавлен!"; } catch (Exception ex) { ViewBag.Message = "Операция отменена.\n" + ex.Message; } return(View()); }
public void InsertContact(Contact contact) { DbContact dbContact = _context.Contacts .FirstOrDefault(c => c.Name == contact.Name && c.AddressLine1 == contact.Address.AddressLine1 && c.AddressLine2 == contact.Address.AddressLine2 && c.AddressLine3 == contact.Address.AddressLine3 && c.City == contact.Address.City && c.State == contact.Address.State && c.Zip == contact.Address.Zip && c.Country == contact.Address.Country); if (dbContact != default) { throw new ContactAlreadyExistsException(dbContact.CreateDomainContact()); } _context.Contacts.Add(contact.MapToDbContact()); _context.SaveChanges(); }
public AddressBookControllerTest() { _dbOptions = new DbContextOptionsBuilder <AddressBookContext>() .UseInMemoryDatabase(databaseName: "in-memory") .Options; using (var dbContext = new AddressBookContext(_dbOptions)) { dbContext.AddRange(AddressBookContextDbSeed.GetData()); dbContext.SaveChanges(); } }
public bool Add(Phonedirectory ob) { try { db.Phonedirectories.Add(ob); db.SaveChanges(); return(true); } catch { return(false); } }
//[WebInvoke(Method = "POST", UriTemplate = "User/Add/{id}")] public bool AddUser(string login, string password) { var newUser = User.Register(login, password); if (newUser != null) { var context = new AddressBookContext(); context.Users.Add(newUser.Value); context.SaveChanges(); return(true); } return(false); }
/// <summary> /// Add User Login details to the database /// </summary> /// <param name="obUserLogin">Pass the object of the Login Class</param> /// <returns>True is successfully registered and False if failed to register</returns> public bool AddUser(UserLogin obUserLogin) { try { db.UserLogins.Add(obUserLogin); db.SaveChanges(); return(true); } catch (Exception) { return(false); } }
public void DeleteAddress(int Id) { using (var context = new AddressBookContext()) { var addressToRemove = context.Adresses.SingleOrDefault(a => a.AdressId == Id); if (addressToRemove != null) { context.Adresses.Remove(addressToRemove); } context.SaveChanges(); } }
public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user try { var user = new User() { UserLogin = model.UserLogin }; user.UserEmail = model.UserEmail; user.UserConfirmedEmail = false; WebSecurity.CreateUserAndAccount(model.UserLogin, model.Password, new { UserName = model.UserName, UserSurname = model.UserSurname, UserMidName = model.UserMidName, UserEmail = model.UserEmail, UserComment = model.UserComment, UserPhoneNumber = model.UserPhoneNumber, UserPassword = model.Password, UserLogin = model.UserLogin, //UserConfirmedEmail=false UserConfirmedEmail = true }); AddressBookContext usersContext = new AddressBookContext(); var userTwo = usersContext.Users.SingleOrDefault(u => u.UserLogin == model.UserLogin); MailHelper mailHelper = new MailHelper(); userTwo.UserConfirmString = mailHelper.GetConfirmString(); usersContext.SaveChanges(); //contactHelper.SendMail(user.UserEmail, userTwo.UserLogin, userTwo.UserConfirmString); //return RedirectToAction("Info", "Home"); return(RedirectToAction("Index", "Home")); } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } } // If we got this far, something failed, redisplay form return(View(model)); }
public bool AddGroupPhone(int UserId, string name) { var context = new AddressBookContext(); var user = context.GetUser(UserId); if (user != null) { user.AddGroupPhone(name); context.SaveChanges(); return(true); } return(false); }
public bool DeleteUser(int id) { var context = new AddressBookContext(); var user = context.GetUser(id); if (user != null) { context.Users.Remove(user); context.SaveChanges(); return(true); } return(false); }
public bool AddSubscriber(int UserId, string firstName, string middleName, string lastName, DateTime?dateOfBirth, byte[] photo, Sex sex, string mail) { var context = new AddressBookContext(); var user = context.GetUser(UserId); if (user != null) { user.AddSubscriber(firstName, middleName, lastName, dateOfBirth, photo, sex, mail); context.SaveChanges(); return(true); } return(false); }
public void UpdateFriend(Friend updatefriend) { using (AddressBookContext dataContext = new AddressBookContext()) { Friend friendToUpdate = dataContext.Friends.Single(t => t.FirstName == updatefriend.FirstName); updatefriend.FirstName = updatefriend.FirstName; updatefriend.LastName = updatefriend.LastName; updatefriend.Street = updatefriend.Street; updatefriend.State = updatefriend.State; updatefriend.Zip = updatefriend.Zip; dataContext.SaveChanges(); } }
public void AddFriend(Friend newFriend) { using (AddressBookContext dataContext = new AddressBookContext()) { Friend friend = new Friend { FirstName = newFriend.FirstName, LastName = newFriend.LastName, Street = newFriend.Street, State = newFriend.State, Zip = newFriend.Zip, }; dataContext.Friends.Add(friend); dataContext.SaveChanges(); } }
public static void Initialize(IServiceProvider services) { using (var context = new AddressBookContext(services.GetRequiredService <DbContextOptions <AddressBookContext> >())) { if (context.People.Count() > 1) { return; // DB has been seeded } using (StreamReader r = new StreamReader("personData.json")) { string json = r.ReadToEnd(); List <Person> people = JsonConvert.DeserializeObject <List <Person> >(json); context.AddRange(people); context.SaveChanges(); } } }
public bool DeleteSubscriber(int UserId, int id) { var context = new AddressBookContext(); var user = context.GetUser(UserId); if (user != null) { var subscriber = user.Subscribers.ToList().Find(s => s.Id == id); if (subscriber != null) { user.RemoveSubscriber(subscriber); } context.SaveChanges(); return(true); } return(false); }
public bool DeleteGroup(int UserId, int id) { var context = new AddressBookContext(); var user = context.GetUser(UserId); if (user != null) { var group = user.Groups.ToList().Find(s => s.Id == id); if (group != null) { user.RemoveGroup(group); } context.SaveChanges(); return(true); } return(false); }
/// <summary> /// Update address, it should be existing in db /// </summary> /// <param name="addressBook"></param> /// <returns></returns> public ActionResult UpdateAddress(AddressBook addressBook) { AddressBookContext aContext = new AddressBookContext(); // Get that addressbook item which got modified var targetAddress = aContext.AddressBook.Where(r => r.ID == addressBook.ID).FirstOrDefault(); // Update could be any of below attribute so reassign every attribute targetAddress.Name = addressBook.Name; targetAddress.Address = addressBook.Address; targetAddress.PhoneNumber = addressBook.PhoneNumber; targetAddress.UpdateDate = DateTime.UtcNow; // Save changes to db aContext.SaveChanges(); // Return partial view with model bind; view with new updated data will be sent in ajax response return(PartialView("_AddressItem", targetAddress)); }
public string DeleteContactDetails(Guid contactId) { string contactCheck = ""; ContactDetail cd = _context.ContactDetails.Where(x => x.Id == contactId).Distinct().FirstOrDefault(); if (cd != null) { _context.ContactDetails.Remove(cd); _context.SaveChanges(); contactCheck = "Contact Deleted !"; } else { contactCheck = "There is no such !"; } return(contactCheck); }
/// <summary> /// Remove certain contact /// </summary> /// <param name="id"></param> /// <returns></returns> public JsonResult DeleteAddress(Guid addressID) { if (addressID == Guid.Empty) { return(Json("")); } AddressBookContext aContext = new AddressBookContext(); // Search and get item we want to remove var targetAddress = aContext.AddressBook.Where(r => r.ID == addressID).FirstOrDefault(); aContext.AddressBook.Remove(targetAddress); // Change in db aContext.SaveChanges(); // Simply return success in ajax response return(Json("success")); }
public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl) { string provider = null; string providerUserId = null; if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId)) { return(RedirectToAction("Manage")); } if (ModelState.IsValid) { // Insert a new user into the database using (AddressBookContext db = new AddressBookContext()) { User user = db.Users.FirstOrDefault(u => u.UserLogin.ToLower() == model.UserLogin.ToLower()); // Check if user already exists if (user == null) { // Insert name into the profile table db.Users.Add(new User { UserLogin = model.UserLogin }); db.SaveChanges(); OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserLogin); OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false); return(RedirectToLocal(returnUrl)); } else { ModelState.AddModelError("UserLogin", "User login already exists. Please enter a different user login."); } } } ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName; ViewBag.ReturnUrl = returnUrl; return(View(model)); }
public AddressBook CreateAddress(AddressBook model) { var addAddress = context.AddressBooks.Add(new AddressBook { FirstName = model.FirstName, SurName = model.SurName, DateOfBirth = model.DateOfBirth, Email = model.Email }); context.SaveChanges(); return(new AddressBook { AddressBookId = addAddress.Entity.AddressBookId, FirstName = addAddress.Entity.FirstName, SurName = addAddress.Entity.SurName, DateOfBirth = addAddress.Entity.DateOfBirth, Email = addAddress.Entity.Email }); }
public void CreateContact(Contact contact, Address address) { using (var context = new AddressBookContext()) { var newContact = new Kontakt(); newContact.Namn = contact.Namn; newContact.Telefon = contact.Telefon; newContact.Epost = contact.Epost; newContact.KontaktTyp = contact.KontaktTyp; var newAddress = new Adress(); newAddress.Gatuadress = address.Gatuadress; newAddress.Postnummer = address.Postnummer; newAddress.Postort = address.Postort; newAddress.KontaktId = contact.Id; context.Kontakts.Add(newContact); context.Adresses.Add(newAddress); context.SaveChanges(); } }
public void CreateContact(CreateEditContact contact, string uploadPathBase) { using (var context = new AddressBookContext(nameOrConnectionString)) { if (contact.File != null && contact.File.ContentLength > 0) { contact.Image = Guid.NewGuid() + Path.GetExtension(contact.File.FileName); contact.File.SaveAs(Path.Combine(uploadPathBase, contact.Image)); } var mapped = new DataAccess.Models.Contact { Id = contact.Id, FullName = contact.FullName, LastName = contact.LastName, Title = contact.Title, Email = contact.Email, OfficeId = contact.OfficeId, DepartmentId = contact.DepartmentId, OrganisationId = contact.OrganisationId, Image = contact.Image, WebLinks = contact.WebLinks .Where(w => !string.IsNullOrWhiteSpace(w.Url)) .Select(w => new WebLink { Id = w.Id, ContactId = w.ContactId, WebLinkTypeId = w.WebLinkTypeId}) .ToList(), PhoneNumbers = contact.PhoneNumbers .Where(p => !string.IsNullOrWhiteSpace(p.PhoneNumber)) .Select(p => new ContactPhoneNumber { Id = p.Id, ContactId = p.ContactId, PhoneNumberTypeId = p.PhoneNumberTypeId}) .ToList() }; context.Contacts.Add(mapped); context.SaveChanges(); // Update the lucene index var luceneUpdate = new CreateUpdateLuceneContact(mapped); luceneUpdate.Execute(); } }
/// <summary> /// To add contact to address book /// Used from ajax calls /// </summary> /// <param name="addressBook"></param> /// <returns></returns> public ActionResult AddAddress(AddressBook addressBook) { AddressBookContext aContext = new AddressBookContext(); // Create new instance of address book or can use same as parameter instance AddressBook aBook = new AddressBook() { // Generate new guid or so called ID ID = Guid.NewGuid(), Name = addressBook.Name, PhoneNumber = addressBook.PhoneNumber, Address = addressBook.Address, CreateDate = DateTime.UtcNow, UpdateDate = DateTime.UtcNow }; aContext.AddressBook.Add(aBook); // Save changes to database aContext.SaveChanges(); // Return partial view; Send data along with view name so that view binds with this data and results are sent in ajax return(PartialView("_AddressItem", aBook)); }
public void UpdateAddress(int id, Address address) { using (var context = new AddressBookContext()) { var updateAddress = context.Adresses.SingleOrDefault(a => a.KontaktId == id); if (updateAddress != null) { updateAddress.Gatuadress = address.Gatuadress; updateAddress.Postnummer = address.Postnummer; updateAddress.Postort = address.Postort; } else { var newAddress = new Adress(); newAddress.Gatuadress = address.Gatuadress; newAddress.Postnummer = address.Postnummer; newAddress.Postort = address.Postort; newAddress.KontaktId = id; context.Adresses.Add(newAddress); } context.SaveChanges(); } }
public void AddPerson(Person person) { _context.Add(person); _context.SaveChanges(); }
public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl) { string provider = null; string providerUserId = null; if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId)) { return RedirectToAction("Manage"); } if (ModelState.IsValid) { // Insert a new user into the database using (AddressBookContext db = new AddressBookContext()) { User user = db.Users.FirstOrDefault(u => u.UserLogin.ToLower() == model.UserLogin.ToLower()); // Check if user already exists if (user == null) { // Insert name into the profile table db.Users.Add(new User { UserLogin = model.UserLogin }); db.SaveChanges(); OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserLogin); OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false); return RedirectToLocal(returnUrl); } else { ModelState.AddModelError("UserLogin", "User login already exists. Please enter a different user login."); } } } ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName; ViewBag.ReturnUrl = returnUrl; return View(model); }
public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user try { var user = new User() { UserLogin = model.UserLogin }; user.UserEmail = model.UserEmail; user.UserConfirmedEmail = false; WebSecurity.CreateUserAndAccount(model.UserLogin, model.Password, new { UserName = model.UserName, UserSurname = model.UserSurname, UserMidName = model.UserMidName, UserEmail = model.UserEmail, UserComment = model.UserComment, UserPhoneNumber = model.UserPhoneNumber, UserPassword = model.Password, UserLogin = model.UserLogin, //UserConfirmedEmail=false UserConfirmedEmail = true }); AddressBookContext usersContext = new AddressBookContext(); var userTwo = usersContext.Users.SingleOrDefault(u => u.UserLogin == model.UserLogin); MailHelper mailHelper = new MailHelper(); userTwo.UserConfirmString = mailHelper.GetConfirmString(); usersContext.SaveChanges(); //contactHelper.SendMail(user.UserEmail, userTwo.UserLogin, userTwo.UserConfirmString); //return RedirectToAction("Info", "Home"); return RedirectToAction("Index", "Home"); } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } } // If we got this far, something failed, redisplay form return View(model); }
public bool AddContact(Contact contact) { _context.Add(contact); _context.SaveChanges(); return(true); }
public void UpdateContact(CreateEditContact contact, string uploadPath) { var mappedContact = new DataAccess.Models.Contact { Id = contact.Id, FullName = contact.FullName, LastName = contact.LastName, Email = contact.Email, Title = contact.Title, DepartmentId = contact.DepartmentId, OfficeId = contact.OfficeId, OrganisationId = contact.OrganisationId, Image = contact.Image, PhoneNumbers = contact.PhoneNumbers .Where(p => !string.IsNullOrWhiteSpace(p.PhoneNumber)) .Select(p => new ContactPhoneNumber { Id = p.Id, ContactId = p.ContactId, PhoneNumber = p.PhoneNumber, PhoneNumberTypeId = p.PhoneNumberTypeId}) .ToList(), WebLinks = contact.WebLinks .Where(w => !string.IsNullOrWhiteSpace(w.Url)) .Select(w => new WebLink { Id = w.Id, ContactId = w.ContactId, Url = w.Url, WebLinkTypeId = w.WebLinkTypeId}) .ToList() }; using (var context = new AddressBookContext(nameOrConnectionString)) { var originalContact = context .Contacts .Include(c => c.PhoneNumbers) .Include(c => c.WebLinks) .FirstOrDefault(c => c.Id == mappedContact.Id); if (originalContact == null) throw new Exception($"No contact with id {mappedContact.Id} exists"); var originalPhoneNumbers = originalContact .PhoneNumbers .ToList(); var deletedPhoneNumbers = originalPhoneNumbers .Except(mappedContact.PhoneNumbers, c => c.Id) .ToList(); var addedPhoneNumbers = mappedContact .PhoneNumbers .Where(p => p.Id == 0) .ToList(); var modifiedPhoneNumbers = mappedContact .PhoneNumbers .Except(deletedPhoneNumbers, c => c.Id) .Except(addedPhoneNumbers, c => c.Id) .ToList(); foreach (var delete in deletedPhoneNumbers) { context.Entry(delete).State = EntityState.Deleted; } foreach (var added in addedPhoneNumbers) { context.Entry(added).State = EntityState.Added; } // Get existing items to update foreach (var modified in modifiedPhoneNumbers) { var existing = context.PhoneNumbers.Find(modified.Id); if (existing == null) continue; context.Entry(existing).CurrentValues.SetValues(modified); } var originalWebLinks = originalContact .WebLinks .ToList(); var deletedWebLinks = originalWebLinks .Except(mappedContact.WebLinks, c => c.Id) .ToList(); var addedWebLinks = mappedContact.WebLinks .Where(c => c.Id == 0) .ToList(); var modifiedWebLinks = mappedContact.WebLinks .Except(deletedWebLinks, c => c.Id) .Except(addedWebLinks, c => c.Id) .ToList(); foreach (var delete in deletedWebLinks) { context.Entry(delete).State = EntityState.Deleted; } foreach (var added in addedWebLinks) { context.Entry(added).State = EntityState.Added; } // Get existing items to update foreach (var modified in modifiedWebLinks) { var existing = context.WebLinks.Find(modified.Id); if (existing == null) continue; context.Entry(existing).CurrentValues.SetValues(modified); } // If we have an image attached if (contact.File != null && contact.File.ContentLength > 0) { var uploadedImageName = Guid.NewGuid() + Path.GetExtension(contact.File.FileName); mappedContact.Image = uploadedImageName; contact.File.SaveAs(Path.Combine(uploadPath, uploadedImageName)); // Delete the original DeleteImage(originalContact.Image, uploadPath); } // If no image is attached it means it should be deleted (if possible) if (string.IsNullOrWhiteSpace(contact.Image)) { // Delete the original DeleteImage(originalContact.Image, uploadPath); } // Update the other properties with the new values context.Entry(originalContact).CurrentValues.SetValues(mappedContact); context.SaveChanges(); // Update the lucene index var luceneUpdate = new CreateUpdateLuceneContact(mappedContact); luceneUpdate.Execute(); } }