コード例 #1
0
        public async Task <object> PostContact([FromBody] Contacts model)
        {
            object result = null; string message = "";

            if (model == null)
            {
                return(BadRequest());
            }
            using (_ctx)
            {
                using (var _ctxTransaction = _ctx.Database.BeginTransaction())
                {
                    try
                    {
                        _ctx.Contacts.Add(model);
                        await _ctx.SaveChangesAsync();

                        _ctxTransaction.Commit();
                        message = "Saved Successfully";
                    }
                    catch (Exception e)
                    {
                        _ctxTransaction.Rollback();
                        e.ToString();
                        message = "Saved Error";
                    }

                    result = new
                    {
                        message
                    };
                }
            }
            return(result);
        }
コード例 #2
0
        public async Task <IActionResult> PutUser(int id, User user)
        {
            if (id != user.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
コード例 #3
0
ファイル: PeopleController.cs プロジェクト: vmarabattuni/pba
        public async Task <IHttpActionResult> PutPeople(int id, People people)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != people.ID)
            {
                return(BadRequest());
            }

            db.Entry(people).State = EntityState.Modified;

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
コード例 #4
0
        public async Task <TEntity> Add(TEntity entity)
        {
            PhoneBookContext.Set <TEntity>().Add(entity);
            await PhoneBookContext.SaveChangesAsync();

            return(entity);
        }
コード例 #5
0
        public async Task <IActionResult> Create([Bind("ContactId,Email,FirstName,LastName,Phone")] Contacts contacts)
        {
            if (ModelState.IsValid)
            {
                _context.Add(contacts);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(contacts));
        }
コード例 #6
0
        public async Task <IActionResult> Create([Bind("ClienteId,Nome,Cpf,Cpnj,Email,TelefoneCelular,TelefoneResidencial,TelefoneComercial,Endereco")] Clientes clientes)
        {
            if (ModelState.IsValid)
            {
                _context.Add(clientes);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(clientes));
        }
コード例 #7
0
        public async Task <IActionResult> Create([Bind("Id,Number,ContactId,TypePhoneId")] Phone phone)
        {
            if (ModelState.IsValid)
            {
                _context.Add(phone);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Details", "Contact", new { id = phone.ContactId }));
            }
            ViewData["TypePhoneId"] = new SelectList(_context.TypePhones, "Id", "Name", phone.TypePhoneId);
            return(PartialView(phone));
        }
コード例 #8
0
        public async Task <ActionResult <Contact> > Post([FromBody] Contact contact)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            _context.Contacts.Add(contact);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("Get", new { id = contact.ContactId }, contact));
        }
コード例 #9
0
 public async Task <ResponseModel <EntryOutputModel> > Edit(int entryId, EntryInputModel entryModel)
 {
     try
     {
         using (var ctx = new PhoneBookContext())
         {
             var existinEntry = ctx.Entries.FirstOrDefault(x => x.EntryId == entryId);
             if (existinEntry.Name != existinEntry.Name || existinEntry.PhoneNumber != entryModel.PhoneNumber || existinEntry.PhoneBookId != entryModel.PhoneBookId)
             {
                 existinEntry.Name        = entryModel.Name;
                 existinEntry.PhoneNumber = entryModel.PhoneNumber;
                 existinEntry.PhoneBookId = entryModel.PhoneBookId;
                 await ctx.SaveChangesAsync();
             }
             return(new ResponseModel <EntryOutputModel> {
                 DataSet = _mapper.Map <Core.Entry, EntryOutputModel>(existinEntry)
             });
         }
     }
     catch (Exception ex)
     {
         return(new ResponseModel <EntryOutputModel> {
             ResponseMessage = Constants.UnexpectedError, IsSuccessful = false
         });
     }
 }
コード例 #10
0
        public IActionResult Post([FromBody] Entry entry)
        {
            _context.Entries.Add(entry);
            _phoneBook.Entries.Add(entry);
            _context.SaveChangesAsync();

            return(Ok());
        }
コード例 #11
0
ファイル: GroupController.cs プロジェクト: IGULOL/PhoneBook
        public async Task <IActionResult> Create([Bind("Id,Name")] Group @group)
        {
            var  name = User.Identity.Name;
            User user = _context.Users.FirstOrDefault(u => u.UserName == name);

            if (user != null)
            {
                ViewData["NameUser"] = user.Surname + " " + user.Name;
            }

            if (ModelState.IsValid)
            {
                _context.Add(@group);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(@group));
        }
コード例 #12
0
        public async Task <IActionResult> Create([Bind("Id,Surname,Name,Patronymic,Address,Job,Position,Email,Skype,GroupId")] Contact contact)
        {
            var name = User.Identity.Name;

            User user = _context.Users.FirstOrDefault(u => u.UserName == name);

            if (ModelState.IsValid && user != null)
            {
                ViewData["NameUser"] = user.Surname + " " + user.Name;

                contact.UserId = user.Id;
                _context.Add(contact);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index", "Home"));
            }
            ViewData["GroupId"] = new SelectList(_context.Groups, "Id", "Name", contact.GroupId);

            return(View(contact));
        }
コード例 #13
0
        public async Task <IActionResult> Add([Bind("Id,Type,Number,PersonId")] Phone phone)
        {
            if (ModelState.IsValid)
            {
                /*if (phone.PersonId != null)
                 * {
                 *  Person person = _context.Persons.First(p => p.Id == phone.Person.Id);
                 *
                 *  phone.Person = person;
                 * }*/


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

                return(new RedirectToActionResult(nameof(Index), "Phones", new { pid = phone.PersonId }));
            }
            ViewData["PersonId"] = phone.PersonId;
            return(View(phone));
        }
コード例 #14
0
        public async Task AddContact(ContactDTO addedContact)
        {
            _customerGuid       = Guid.NewGuid();
            _contactDetailsGuid = Guid.NewGuid();

            Customer customer = new Customer
            {
                Id        = _customerGuid,
                FirstName = addedContact.FirstName,
                LastName  = addedContact.LastName,
                Gender    = addedContact.Gender
            };

            ContactDetails details = new ContactDetails
            {
                Id          = _contactDetailsGuid,
                CustomerId  = _customerGuid,
                Email       = addedContact.Email,
                MobilePhone = addedContact.MobilePhone,
                HomePhone   = addedContact.HomePhone,
                FacebookId  = addedContact.FacebookId
            };

            Address address = new Address
            {
                ContactDetailsId = _contactDetailsGuid,
                City             = addedContact.City,
                Province         = addedContact.Province,
                Street           = addedContact.Street,
                ZipCode          = addedContact.ZipCode
            };

            await _dbConext.Customers.AddAsync(customer);

            await _dbConext.ContactDetails.AddAsync(details);

            await _dbConext.Address.AddAsync(address);

            await _dbConext.SaveChangesAsync();
        }
コード例 #15
0
        public async Task <IActionResult> Create([Bind("Type,FirstName,LastName,Image,Address,Description")] Person person)
        {
            if (ModelState.IsValid)
            {
                person.Picture = person.Image?.FileName;
                var Image = person.Image;
                person.Image = null;
                string ImageName = System.IO.Path.GetFileName(Image.FileName);
                var    filePath  = Path.Combine("C:\\Users\\Mohammad\\source\\repos\\Phonebook\\wwwroot\\images\\", ImageName);

                using (var stream = System.IO.File.Create(filePath))
                {
                    Image.CopyTo(stream);
                }

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

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

            return(View(person));
        }
コード例 #16
0
        public async Task <IActionResult> Add(ContactDto contactDto)
        {
            var contactsWithSamePhoneCount = db.Contacts
                                             .Select(n => n)
                                             .Count(c => c.PhoneNumber.Contains(contactDto.PhoneNumber));

            ViewData["PhonesCount"] = contactsWithSamePhoneCount;

            if (contactsWithSamePhoneCount > 0)
            {
                return(View(contactDto));
            }

            if (ModelState.IsValid)
            {
                await db.Contacts.AddAsync(contactDto.ToModel());

                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(contactDto));
        }
コード例 #17
0
        // PUT: odata/PeopleOdata(5)
        public async Task <IHttpActionResult> Put([FromODataUri] int key, Delta <People> patch)
        {
            Validate(patch.GetEntity());

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            People people = await db.Peoples.FindAsync(key);

            if (people == null)
            {
                return(NotFound());
            }

            patch.Put(people);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PeopleExists(key))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Updated(people));
        }
コード例 #18
0
        public async Task <ResponseModel <EntryOutputModel> > Create(EntryInputModel entryModel)
        {
            try
            {
                using (var ctx = new PhoneBookContext())
                {
                    var entity = _mapper.Map <EntryInputModel, Core.Entry>(entryModel);
                    ctx.Entries.Add(entity);
                    await ctx.SaveChangesAsync();

                    return(new ResponseModel <EntryOutputModel> {
                        DataSet = _mapper.Map <Core.Entry, EntryOutputModel>(entity)
                    });
                }
            }
            catch (Exception ex)
            {
                return(new ResponseModel <EntryOutputModel> {
                    ResponseMessage = Constants.UnexpectedError, IsSuccessful = false
                });
            }
        }
コード例 #19
0
 public async Task <ResponseModel <PhoneBookOutputModel> > Edit(int id, PhoneBookInputModel phoneBookModel)
 {
     try
     {
         using (var ctx = new PhoneBookContext())
         {
             var existingPhoneBook = ctx.PhoneBooks.FirstOrDefault(x => x.PhoneBookId == id);
             if (existingPhoneBook.Name != phoneBookModel.Name)
             {
                 existingPhoneBook.Name = phoneBookModel.Name;
                 await ctx.SaveChangesAsync();
             }
             return(new ResponseModel <PhoneBookOutputModel> {
                 DataSet = _mapper.Map <Core.PhoneBook, PhoneBookOutputModel>(existingPhoneBook)
             });
         }
     }
     catch (Exception ex)
     {
         return(new ResponseModel <PhoneBookOutputModel> {
             ResponseMessage = Constants.UnexpectedError, IsSuccessful = false
         });
     }
 }
コード例 #20
0
        public async Task Insert <T>(T model) where T : class
        {
            await _db.Set <T>().AddAsync(model);

            await _db.SaveChangesAsync();
        }
コード例 #21
0
 public async Task SaveAsync()
 {
     await _context.SaveChangesAsync();
 }
コード例 #22
0
 public async Task CompleteAsync()
 {
     await _context.SaveChangesAsync();
 }
コード例 #23
0
 public async Task InsertContact(Contact ctt)
 {
     _context.Add(ctt);
     await _context.SaveChangesAsync();
 }