public async Task <IActionResult> PutPerson(int id, Person person)
        {
            if (id != person.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Пример #2
0
        public async Task <PersonRecord> CreateAsync(PersonRecord entity)
        {
            _context.People.Add(entity);
            await _context.SaveChangesAsync();

            return(entity);
        }
Пример #3
0
        public async Task <IHttpActionResult> PutPerson(int id, Person person)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

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

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
Пример #4
0
        public async Task <IActionResult> Create([Bind("Id,FirstName,LastName,Title,Age,Address,City,Phone,Email")] Person person)
        {
            if (ModelState.IsValid)
            {
                _context.Add(person);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(person));
        }
Пример #5
0
        public async Task <IActionResult> Create([Bind("PId,PName,PAge")] Person person)
        {
            if (ModelState.IsValid)
            {
                _context.Add(person);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(person));
        }
Пример #6
0
        public async Task <IActionResult> Create([Bind("PersonId,FirstName,LastName,BirthDate,IsApproved,Role")] Person person)
        {
            if (ModelState.IsValid)
            {
                _context.Add(person);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(person));
        }
        public async Task <IActionResult> PutPerson(int id, PersonDTO personDTO)
        {
            if (id != personDTO.Id)
            {
                return(BadRequest());
            }

            var person = await _context.People
                         .Include(p => p.Address)
                         .Include(p => p.Interests)
                         .FirstOrDefaultAsync(p => p.Id == id);

            person.FirstName       = personDTO.FirstName;
            person.MiddleName      = personDTO.MiddleName;
            person.LastName        = personDTO.LastName;
            person.Address         = personDTO.Address;
            person.PersonAddressId = personDTO.PersonAddressId;
            person.DOB             = new DateTime(personDTO.DOB.Year, personDTO.DOB.Month, personDTO.DOB.Day);
            person.PathToAvatar    = personDTO.PathToAvatar;
            person.IsFavorite      = personDTO.IsFavorite;

            _context.Entry(person).State = EntityState.Modified;
            if (personDTO.Address != null)
            {
                _context.Entry(personDTO.Address).State = EntityState.Modified;
            }
            if (personDTO.Interests != null)
            {
                foreach (var interest in personDTO.Interests)
                {
                    _context.Entry(interest).State = EntityState.Modified;
                }
            }

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

            return(NoContent());
        }
        public override async Task <AddressAdditionResponse> AddUserAddress(AddressAdditionRequest request, ServerCallContext context)
        {
            // var userAddress = new AddressData { Name = request.Name, Address = request.Address, Enrollment = request.Enrollment };
            var userAddress = GetAddressData(request);

            _personDbContext.Add(userAddress);
            await _personDbContext.SaveChangesAsync();

            var results = new AddressAdditionResponse {
                Message = "Address Save Successfully."
            };

            return(results);
        }
Пример #9
0
        public override async Task <HealthResponse> AddPersonHealthData(HealthRequest request, ServerCallContext context)
        {
            var healthData = new HealthData {
                Name = request.Name, HealthParameter1 = request.HealthParameter1, HealthParameter2 = request.HealthParameter2
            };

            _personDbContext.Health.Add(healthData);
            await _personDbContext.SaveChangesAsync();

            var results = new HealthResponse {
                Message = "Health Data Save Successfully."
            };

            return(results);
        }
Пример #10
0
        public static void Initialize(PersonDbContext context)
        {
            if (context.People.Any())
            {
                return;
            }

            var people = new List <PersonRecord>
            {
                new PersonRecord {
                    FirstName = "Bill", LastName = "Gates", DateOfBirth = new DateTime(1955, 10, 28), Street = "Microsoft way", City = "Seattle", State = "Washington", PostalCode = "98134", PictureName = "a4759609-0051-4cd5-a84b-1d4efa743c97.jpg"
                },
                new PersonRecord {
                    FirstName = "Mark", LastName = "Zuckerberg", DateOfBirth = new DateTime(1984, 5, 14), Street = "Facebook street", City = "White Plains", State = "New York", PostalCode = "10016", PictureName = "4dc720fc-cb7b-4f94-a004-21101c1a64e7.jpg"
                },
                new PersonRecord {
                    FirstName = "Jeff", LastName = "Bezos", DateOfBirth = new DateTime(1964, 1, 12), Street = "Amazon Prime way", City = "Albuquerque", State = "New Mexico", PostalCode = "87110", PictureName = "f1c1d561-a45d-4d8c-bb37-4641de13a64e.jpg"
                },
                new PersonRecord {
                    FirstName = "Steve", LastName = "Jobs", DateOfBirth = new DateTime(1955, 02, 24), Street = "Apple Parkway", City = "San Francisco", State = "California", PostalCode = "90201", PictureName = "bc3b154e-135c-4fd3-aee0-e9dfceee8286.jpg"
                },
                new PersonRecord {
                    FirstName = "Elon", LastName = "Musk", DateOfBirth = new DateTime(1971, 6, 28), Street = "Tesla street way", City = "Pretoria", State = "South Africa", PostalCode = "Unknown", PictureName = "2702f7df-bce7-4f3b-8741-efe32215435b.jpg"
                },
                new PersonRecord {
                    FirstName = "Tim", LastName = "Cook", DateOfBirth = new DateTime(1960, 11, 1), Street = "Apple Parkway", City = "Mobile", State = "Alabama", PostalCode = "36611", PictureName = "3271aeb1-2db7-470f-acac-b375a9002cc4.jpg"
                },
            };

            people.ForEach(p => context.People.Add(p));
            context.SaveChangesAsync();
        }
        public async Task <IActionResult> AddPersonToDB([FromBody] Person personWithPresent)
        {
            personWithPresent.ID = _context.Persons.Max(p => p.ID) + 1;
            _context.Persons.Add(personWithPresent);
            await _context.SaveChangesAsync();

            return(Ok());
        }
        public async Task <ActionResult <Person> > Post([FromBody] Person persone)
        {
            if (int.TryParse(Request.Headers["X-UserId"], out int userId))
            {
                if (PersoneExists(userId))
                {
                    return(BadRequest("Persone already exists."));
                }

                persone.UserId = userId;
                await _context.Persons.AddAsync(persone);

                await _context.SaveChangesAsync();

                return(CreatedAtAction(nameof(Post), new { id = persone.Id }, persone));
            }

            return(BadRequest());
        }
Пример #13
0
        public async Task Update(PersonUpdateDTO updateDTO)
        {
            var personFromDb = _db.Person.FirstOrDefault(p => p.id == updateDTO.id);

            personFromDb.name          = updateDTO.name;
            personFromDb.state         = updateDTO.state;
            personFromDb.streetAddress = updateDTO.streetAddress;
            personFromDb.city          = updateDTO.city;
            personFromDb.postalCode    = updateDTO.postalCode;

            await _db.SaveChangesAsync();
        }
        public async Task <IActionResult> AddPerson([FromBody] Person person)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            await db.AddAsync(person);

            await db.SaveChangesAsync();

            return(Ok(person.Id));
        }
        public override async Task <AddressAdditionResponse> AddUserAddress(AddressAdditionRequest request, ServerCallContext context)
        {
            var userAddress = GetAddressData(request);

            try
            {
                if (userAddress.Enrollment == "Bank")
                {
                    var trailer = new Metadata()
                    {
                        { "BadValue", "Enrollment type is Bank" },
                        { "Field", "Enrollment" },
                        { "Message", "Bank enrollment is stopped temporarily" }
                    };
                    throw new RpcException(new Status(StatusCode.Cancelled, "Bank enrollment is stopped temporarily"), trailer);
                }
                _personDbContext.Add(userAddress);
                await _personDbContext.SaveChangesAsync();
            }
            catch (InvalidOperationException)
            {
                var trailer = new Metadata()
                {
                    { "CorelationId", Guid.NewGuid().ToString() },
                    { "Message", "Unable to save the Data inside Database." }
                };
                throw new RpcException(new Status(StatusCode.Internal, "Internal Error"), trailer);
            }
            catch (RpcException rpcError)
            {
                throw rpcError;
            }

            var results = new AddressAdditionResponse {
                Message = "Address Save Successfully."
            };

            return(results);
        }
        public async Task <IActionResult> PutPerson(int id, Person person)
        {
            if (id != person.Id)
            {
                return(BadRequest());
            }

            person.IsActive = true;
            var local = _context.Set <Person>()
                        .Local
                        .FirstOrDefault(entry => entry.Id.Equals(id));

            if (local != null)
            {
                // detach
                _context.Entry(local).State = EntityState.Detached;
            }
            _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> PutPersonInterest(int id, PersonInterest personInterest)
        {
            personInterest.Id = id;

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

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

            return(NoContent());
        }
        private async void btn_delete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are Yor Sure to Delete this Record?", "EF Crud Operation,", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                using (PersonDbContext db = new PersonDbContext())
                {
                    var entry = db.Entry(people);
                    if (entry.State == EntityState.Detached)
                    {
                        db.People.Attach(people);
                    }
                    db.People.Remove(people);
                    await db.SaveChangesAsync();

                    DataLoad();
                    Clear();
                    MessageBox.Show("Deleted Successfully");
                }
            }
        }
Пример #19
0
        public async Task UpdateItem(Person item)
        {
            try
            {
                var personToUpdate = _personDbContext.People
                                     .Where(p => p.PersonId == item.PersonId).FirstOrDefault();

                if (personToUpdate != null)
                {
                    _personDbContext.Entry(personToUpdate).CurrentValues.SetValues(item);
                }

                await _personDbContext.SaveChangesAsync();

                _logger.LogInformation($"Updated details for item with id {item.PersonId}");
            }
            catch (Exception ex)
            {
                _logger.LogError($"An error occured while updating item with Id {item.PersonId}. Exception details :{ex.Message}");
            }
        }
        private async void btn_save_Click(object sender, EventArgs e)
        {
            people.Name     = txbx_name.Text.Trim();
            people.Surname  = txbx_surname.Text.Trim();
            people.Email    = txbx_email.Text.Trim();
            people.Password = txbx_password.Text.Trim();

            List <ValidationResult> validationResults = new List <ValidationResult>();

            ValidationContext validationContext = new ValidationContext(people);

            if (!Validator.TryValidateObject(people, validationContext, validationResults, true))
            {
                lbl_errors.Text = string.Empty;
                foreach (ValidationResult item in validationResults)
                {
                    lbl_errors.Text += item.ErrorMessage + "\n";
                }
            }
            else
            {
                using (PersonDbContext db = new PersonDbContext())
                {
                    if (people.Id == 0) //insert
                    {
                        db.People.Add(people);
                    }
                    else //update
                    {
                        db.Entry(people).State = EntityState.Modified;
                    }
                    await db.SaveChangesAsync();
                }
                Clear();
                DataLoad();
                MessageBox.Show("Submitted Successfully");
            }
        }
Пример #21
0
 public async Task <int> SaveChanges()
 {
     return(await Db.SaveChangesAsync());
 }