コード例 #1
0
        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());
        }
コード例 #2
0
        public async Task <Result <bool> > AddUserAsync(string login, string password)
        {
            if (!db.Users.Any(u => u.Login == login))
            {
                // validate data
                var newUser = User.Register(login, password);
                if (newUser.Succeeded)
                {
                    // perform additional actions
                    CreateGroup(newUser.Value, "Семья");
                    CreateGroup(newUser.Value, "Рабочий");
                    CreateGroup(newUser.Value, "Друзья");
                    CreateGroup(newUser.Value, "Общие");

                    CreateGroupPhone(newUser.Value, "Домашний");
                    CreateGroupPhone(newUser.Value, "Рабочий");
                    CreateGroupPhone(newUser.Value, "Мобильный");

                    CreateGroupAddress(newUser.Value, "Домашний");
                    CreateGroupAddress(newUser.Value, "Рабочий");

                    db.Users.Add(newUser.Value);
                    await db.SaveChangesAsync();

                    return(Result <bool> .Success(true));
                }
                return(Result <bool> .Fail(newUser.Errors));
            }
            return(Result <bool> .Fail(new string[] { "Such User exists" }));
        }
コード例 #3
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());
        }
コード例 #4
0
        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());
        }
コード例 #5
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());
        }
コード例 #6
0
        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());
        }
コード例 #7
0
        public async Task <ActionResult <PersonModel> > Post([FromBody] PersonModel person)
        {
            if (ModelState.IsValid == true)
            {
                _context.People.Add(person);
                await _context.SaveChangesAsync();
            }

            return(CreatedAtAction(nameof(Get), new { id = person.Id }, person));
        }
コード例 #8
0
        public async Task <IActionResult> Create([Bind("PersonID,FirstName,LastName")] Person person)
        {
            if (ModelState.IsValid)
            {
                _context.Add(person);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(person));
        }
コード例 #9
0
        public async Task <IActionResult> Create([Bind("Id,FirstName,LastName,Email,Avatar,FileName,Adress1,Address2,City,ZipCode,Phone,DateAdded")] AddressBookEntry addressBookEntry)
        {
            if (ModelState.IsValid)
            {
                _context.Add(addressBookEntry);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(addressBookEntry));
        }
コード例 #10
0
        public async Task <IActionResult> Create([Bind("EmployeeID,Name,OrgID,Phone,Mobile,Office")] Employee employee)
        {
            if (ModelState.IsValid)
            {
                _context.Add(employee);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(employee));
        }
コード例 #11
0
        public async Task <IActionResult> Create([Bind("AddressID,PhoneNumber,Email,StreetName,HomeNumber,PostalCode,City,PersonID")] Address address)
        {
            if (ModelState.IsValid)
            {
                _context.Add(address);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["PersonID"] = new SelectList(_context.Person, "PersonID", "FirstName", address.PersonID);
            return(View(address));
        }
コード例 #12
0
        public async Task <IActionResult> Create(int OrgId, [Bind("Id,Name,PhoneNumber,Address,OrganizationId")] Person person)
        {
            person.OrganizationId = OrgId;
            if (ModelState.IsValid)
            {
                _context.Add(person);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Details", "Organizations", new { id = OrgId }));
            }
            return(View(person));
        }
コード例 #13
0
        public async Task <IActionResult> Create([Bind("Id,Name,Address,PhoneNumber")] Organization organization)
        {
            if (ModelState.IsValid)
            {
                _context.Add(organization);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(organization));
        }
コード例 #14
0
        public async Task <Result <bool> > AddAbonentGroupAsync(Abonent abonent, Group group)
        {
            if (!abonent.Groups.Any(p => p.Id == group.Id))
            {
                var newAddress = AbonentGroup.Create(abonent, group);
                if (newAddress.Succeeded)
                {
                    abonent.AddAbonentGroup(newAddress.Value);
                    await db.SaveChangesAsync();

                    return(Result <bool> .Success(true));
                }
                return(Result <bool> .Fail(newAddress.Errors));
            }
            return(Result <bool> .Fail(new string[] { "Such AbonentGroup exists" }));
        }
コード例 #15
0
        public async Task <Result <bool> > AddAddressAsync(Abonent abonent, GroupAddress groupAddress, string information)
        {
            if (!abonent.Addresses.Any(p => p.Information == information))
            {
                var newAddress = Address.Create(groupAddress, information);
                if (newAddress.Succeeded)
                {
                    abonent.AddAddress(newAddress.Value);
                    await db.SaveChangesAsync();

                    return(Result <bool> .Success(true));
                }
                return(Result <bool> .Fail(newAddress.Errors));
            }
            return(Result <bool> .Fail(new string[] { "Such Address exists" }));
        }
コード例 #16
0
        public async Task AddNewItem(Address addressItem)
        {
            await using var db = new AddressBookContext();
            await db.Addresses.AddAsync(addressItem);

            await db.SaveChangesAsync();
        }
コード例 #17
0
        public async Task <Result <bool> > AddGroupAddressAsync(User user, string name)
        {
            if (!user.GroupAddresses.Any(g => g.Name == name))
            {
                var newGroupAddress = GroupAddress.Create(name);
                if (newGroupAddress.Succeeded)
                {
                    user.AddGroupAddress(newGroupAddress.Value);
                    await db.SaveChangesAsync();

                    return(Result <bool> .Success(true));
                }
                return(Result <bool> .Fail(newGroupAddress.Errors));
            }
            return(Result <bool> .Fail(new string[] { "Such GroupAddress exists" }));
        }
コード例 #18
0
        public async Task <Result <bool> > AddPhoneAsync(Abonent abonent, GroupPhone phoneGroup, string number)
        {
            if (!abonent.Phones.Any(p => p.Number == number))
            {
                var newPhone = Phone.Create(phoneGroup, number);
                if (newPhone.Succeeded)
                {
                    abonent.AddPhone(newPhone.Value);
                    await db.SaveChangesAsync();

                    return(Result <bool> .Success(true));
                }
                return(Result <bool> .Fail(newPhone.Errors));
            }
            return(Result <bool> .Fail(new string[] { "Such Phone exists" }));
        }
コード例 #19
0
 public async Task Delete(Address address)
 {
     using (var db = new AddressBookContext())
     {
         db.Addresses.Attach(address);
         db.Addresses.Remove(address);
         await db.SaveChangesAsync();
     }
 }
コード例 #20
0
 public async Task Delete(Person person)
 {
     using (var db = new AddressBookContext())
     {
         db.Persons.Attach(person);
         db.Persons.Remove(person);
         await db.SaveChangesAsync();
     }
 }
コード例 #21
0
        public async Task <Address> Create(Address address)
        {
            using (var db = new AddressBookContext())
            {
                db.Addresses.Add(address);
                await db.SaveChangesAsync();

                return(address);
            }
        }
コード例 #22
0
        public async Task <ActionResult <AddressModel> > Post([FromBody] AddressModel address)
        {
            if (ModelState.IsValid == true)
            {
                _context.Add(address);
                await _context.SaveChangesAsync();
            }

            return(CreatedAtAction(nameof(Get), new { id = address.Id }, address));
        }
コード例 #23
0
        public async Task <Person> Create(Person person)
        {
            using (var db = new AddressBookContext())
            {
                db.Persons.Add(person);
                await db.SaveChangesAsync();

                return(person);
            }
        }
コード例 #24
0
        public async Task <IActionResult> Create([Bind("Id,FirstName,LastName,Email,Avatar,Address1,Address2,City,State,ZipCode,Phone,Birthday,Category")] Address address, IFormFile avatar)
        {
            if (ModelState.IsValid)
            {
                address.DateAdded = DateTime.Now;

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

                _context.Add(address);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(address));
        }
コード例 #25
0
        public async Task DeleteItem(int id)
        {
            await using var db = new AddressBookContext();
            var address = new Address {
                Id = id
            };

            db.Addresses.Attach(address);
            db.Addresses.Remove(address);
            await db.SaveChangesAsync();
        }
コード例 #26
0
        public async Task <Result <bool> > AddAbonentAsync(User user, string firstName, string middleName, string lastName, DateTime?dateOfBirth, byte[] photo, Sex sex, string mail)
        {
            if (!user.Abonents.Any(g => g.FirstName == firstName && g.MiddleName == middleName && g.LastName == lastName))
            {
                var newAbonent = Abonent.Create(firstName,
                                                middleName,
                                                lastName,
                                                dateOfBirth,
                                                photo,
                                                sex,
                                                mail);
                if (newAbonent.Succeeded)
                {
                    user.AddAbonent(newAbonent.Value);
                    await db.SaveChangesAsync();

                    return(Result <bool> .Success(true));
                }
                return(Result <bool> .Fail(newAbonent.Errors));
            }
            return(Result <bool> .Fail(new string[] { "Such Abonent exists" }));
        }
コード例 #27
0
        public async Task EditItem(Address addressItem)
        {
            await using var db = new AddressBookContext();
            var itemToEdit = await db.Addresses.FindAsync(addressItem.Id);

            if (itemToEdit != null)
            {
                itemToEdit.FullName        = addressItem.FullName;
                itemToEdit.PhoneNumber     = addressItem.PhoneNumber;
                itemToEdit.Email           = addressItem.Email;
                itemToEdit.PhysicalAddress = addressItem.PhysicalAddress ?? string.Empty;
                await db.SaveChangesAsync();
            }
        }
コード例 #28
0
        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);
            }
        }
コード例 #29
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);
            }
        }
コード例 #30
0
ファイル: Repository.cs プロジェクト: sima84/AddressBook
 public async Task <int> SaveAsync()
 {
     return(await context.SaveChangesAsync());
 }