public async Task <Contact> AddContactAsync(ContactDto contactDto)
        {
            var contact = _mapper.Map <Contact>(contactDto);
            await _dbContext.AddAsync(contact);

            await _dbContext.SaveChangesAsync();

            return(contact);
        }
Beispiel #2
0
        public async Task <User> AddUserAsync(User user)
        {
            var existingUser = await _dbContext.Users
                               .AsNoTracking()
                               .FirstOrDefaultAsync(u => u.EmailAddress == user.EmailAddress);

            if (existingUser == null)
            {
                await _dbContext.AddAsync(user);

                await _dbContext.SaveChangesAsync();

                return(user);
            }
            return(null);
        }
Beispiel #3
0
        public async Task <IActionResult> ContactEditPost(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            ContactPerson contactPersonUpdated = await _context.ContatPersons
                                                 .SingleAsync(_cp => _cp.Id == id);

            if (await TryUpdateModelAsync <ContactPerson>(
                    contactPersonUpdated, "",
                    _cp => _cp.Name, _cp => _cp.Surname,
                    _cp => _cp.Email, _cp => _cp.Phone,
                    _cp => _cp.GenderType, _cp => _cp.Birthdate
                    ))
            {
                try
                {
                    await _context.SaveChangesAsync();

                    return(RedirectToAction("ContactEdit"));
                }
                catch (DbUpdateException /* ex */)
                {
                    //Log the error (uncomment ex variable name and write a log.)
                    ModelState.AddModelError("", "Unable to save changes. " +
                                             "Try again, and if the problem persists, " +
                                             "see your system administrator.");
                }
            }

            return(View(contactPersonUpdated));
        }