public async Task UpdateAsync(Contact contact)
        {
            if (_ctx.Entry(contact).State != EntityState.Modified)
            {
                _ctx.Entry(contact).State = EntityState.Modified;
            }

            await Task.CompletedTask;
        }
Exemple #2
0
        public void Delete(T entity)
        {
            var dbSet = context.Set <T>();

            if (context.Entry(entity).State == EntityState.Detached)
            {
                dbSet.Attach(entity);
            }
            dbSet.Remove(entity);
        }
Exemple #3
0
        public User GetUser(int id)
        {
            var user = db.Users.Find(id);

            if (user != null)
            {
                db.Entry(user).Collection(s => s.Abonents).Load();
                db.Entry(user).Collection(s => s.GroupAddresses).Load();
                db.Entry(user).Collection(s => s.Groups).Load();
                db.Entry(user).Collection(s => s.GroupPhones).Load();
                return(user);
            }
            return(null);
        }
Exemple #4
0
        public async Task <IActionResult> PutAddress([FromRoute] int id, [FromBody] Address address)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != address.Id)
            {
                return(BadRequest());
            }

            _context.Entry(address).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AddressExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutContacts(int id, Contacts contacts)
        {
            if (id != contacts.ContactId)
            {
                return(BadRequest());
            }

            _context.Entry(contacts).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ContactsExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutPerson([FromRoute] int id, [FromBody] Person person)
        {
            if (id != person.PersonID)
            {
                return(BadRequest());
            }

            context.Entry(person).State = EntityState.Modified;

            try
            {
                await context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PersonExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutGroup([FromRoute] int id, [FromBody] Group group)
        {
            if (id != group.GroupID)
            {
                return(BadRequest());
            }

            // Set status of provided group to modified. This will trigger
            // an update in the database.
            context.Entry(group).State = EntityState.Modified;

            try
            {
                await context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!GroupExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemple #8
0
        public async Task <IActionResult> PutAddressBook(int id, AddressBook addressBook)
        {
            if (id != addressBook.ID)
            {
                return(BadRequest());
            }

            _context.Entry(addressBook).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AddressBookExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemple #9
0
 public ActionResult Edit([Bind(Include = "ContactId,FirstName,LastName,Email,Mobile,Landline,Notes")] Contact contact)
 {
     if (ModelState.IsValid)
     {
         db.Entry(contact).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(contact));
 }
Exemple #10
0
        public async Task <IActionResult> Put(int id, [FromBody] PersonModel person)
        {
            if (id != person.Id)
            {
                return(BadRequest());
            }

            _context.Entry(person).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(NoContent());
        }
Exemple #11
0
        public async Task <Person> Update(Person person)
        {
            using (var db = new AddressBookContext())
            {
                db.Persons.Attach(person);

                var updatedPerson = db.Entry(person);
                updatedPerson.State = EntityState.Modified;

                await db.SaveChangesAsync();

                return(updatedPerson.Entity);
            }
        }
        public async Task <Address> Update(Address address)
        {
            using (var db = new AddressBookContext())
            {
                db.Addresses.Attach(address);

                var updatedAddress = db.Entry(address);
                updatedAddress.State = EntityState.Modified;

                await db.SaveChangesAsync();

                return(updatedAddress.Entity);
            }
        }
Exemple #13
0
        public Abonent GetAbonent(int UserId, int AbonentId)
        {
            var Abonent = db.Users.Find(UserId).Abonents.ToList().Find(s => s.Id == AbonentId);

            db.Entry(Abonent).Collection(s => s.Addresses).Load();
            db.Entry(Abonent).Collection(s => s.Phones).Load();
            db.Entry(Abonent).Collection(s => s.Groups).Load();
            return(Abonent);
        }
Exemple #14
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,FirstName,LastName,Email,Avatar,Address1,Address2,City,State,ZipCode,Phone,DateAdded")] Address address, IFormFile avatar)
        {
            if (id != address.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (avatar != null)
                    {
                        address.Avatar   = AvatarHelper.PutImage(avatar);
                        address.FileName = avatar.FileName;
                    }

                    _context.Update(address);
                    if (avatar == null)
                    {
                        _context.Entry(address).Property(p => p.Avatar).IsModified = false;
                    }
                    await _context.SaveChangesAsync();

                    _context.Update(address);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AddressExists(address.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(address));
        }
        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();
            }
        }