public void DeleteFamily(int FamilyId) { Family family = DBcontext.Family.Find(FamilyId); DBcontext.Family.Remove(family); DBcontext.SaveChanges(); }
public void DeleteUser(int UserId) { User user = DBcontext.User.Find(UserId); DBcontext.User.Remove(user); DBcontext.SaveChanges(); }
public IActionResult Register([FromBody] UserModel userModel) { 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 })); } User user = Mapper.Map <User>(userModel); _dbContext.User.Add(user); _dbContext.SaveChanges(); return(Created($"api/user/{user.Id}", user)); } catch (Exception ex) { return(StatusCode(500, new { message = ex.Message })); } }
public IActionResult Post([FromBody] FamilyModel familyModel) { 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).FirstOrDefault(); if (user != null) { Family family = Mapper.Map <Family>(familyModel); _dbContext.Family.Add(family); _dbContext.SaveChanges(); return(Created($"api/family/{family.Id}", family)); } else { return(NotFound(new { message = "user does not exist" })); } } catch (Exception ex) { return(StatusCode(500, new { message = ex.Message })); } }