public IActionResult Put([FromBody] FamilyModel familyModel, [FromRoute] int id)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    var errors = new List <string>();
                    foreach (var state in ModelState)
                    {
                        foreach (var error in state.Value.Errors)
                        {
                            errors.Add(error.ErrorMessage);
                        }
                    }
                    return(BadRequest(new { message = errors }));
                }

                var user = _dbContext.User.Where(x => x.Id == familyModel.UserId).Include(u => u.Family).FirstOrDefault();

                if (user != null)
                {
                    Family family = user.Family.Where(f => f.Id == id).FirstOrDefault();

                    family.Name                    = familyModel.Name;
                    family.Age                     = familyModel.Age;
                    family.Relationship            = familyModel.Relationship;
                    family.Phone                   = familyModel.Phone;
                    _dbContext.Entry(family).State = EntityState.Modified;
                    _dbContext.SaveChanges();
                    //return Created($"api/family/{family.Id}", family);
                    return(Ok(family));
                }
                else
                {
                    return(NotFound(new { mesage = "user does not exist" }));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(500, new { message = ex.Message }));
            }
        }
 public void UpdateFamily(Family family)
 {
     DBcontext.Entry(family).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
     DBcontext.SaveChanges();
 }
예제 #3
0
 public void UpdateUser(User user)
 {
     DBcontext.Entry(user).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
     DBcontext.SaveChanges();
 }