public int HaveUserHobby(int hobbyId) { User_Hobby userHobby = _hr.HaveUserHobby(hobbyId); int haveHobby = -1; if (userHobby != null) { haveHobby = _hm.UpdateUserHobby(hobbyId); return(haveHobby); } return(haveHobby); }
//UPDATE public void Update(User_Hobby entity) { var _userhobby = _userhobbyContext.User_Hobbies.Find(entity.Id); if (_userhobby == null) { throw new AppException("Hobby not found"); } if (_userhobby.HobbyId != entity.HobbyId) { if (_userhobbyContext.User_Hobbies.Any(x => x.HobbyId == entity.HobbyId) && _userhobbyContext.User_Hobbies.Any(x => x.UserId == entity.UserId)) { throw new AppException("This combination is already taken"); } } _userhobby.HobbyId = entity.HobbyId; _userhobby.UserId = entity.UserId; _userhobbyContext.User_Hobbies.Update(_userhobby); _userhobbyContext.SaveChanges(); }
//ADD public void Add(User_Hobby entity) { _userhobbyContext.User_Hobbies.Add(entity); _userhobbyContext.SaveChanges(); }