Rajesh Sut

Rajesh Sut

  • NA
  • 3
  • 1.6k

Calling a function from repository to controller in asp.net

Mar 7 2018 1:18 PM
Hello everybody, I'm currently an intern, developing a company's own product, my current task is to build a part of website for managing users, It's basically an angular crud app with asp.net backend. Backend is written primarily for android and ios versions of app, so I'm building an api using parts of that backend for my task...
 
So here's the deal, let me give you an example of what I'm trying to ask, because I'm not too good with the asp.net framework yet...
 
In my UserController I'm going to set get, post, patch and delete methods by getting for example
  1. GetAllUsersByOrganization from UserBussinessObject..   
  2. [Route("{id}/users"), HttpGet]  
  3. public IHttpActionResult GetUsers(int id)  
  4. { var result = UserBusinessObject.GetAllUsersByOrganization(id);  
  5. return Ok(new { data = result });  
  6. }  
But here's the deal, if I have in UserBussinessObject a function like this
  1. /// Update user by organization manager  
  2. /// </summary>  
  3. /// <param name="userUpdateModel"></param>  
  4. /// <returns></returns>  
  5. [LogException]  
  6. public int UpdateUserByManager([Required]User userUpdateModel, User currentuser)  
  7. {  
  8. // Find user to update User  
  9. userToUpdate = UserRepository.GetUserById(userUpdateModel.UserId);  
  10. if (userToUpdate != null)  
  11. // Update user in database  
  12. UserRepository.UpdateUser(userToUpdate);  
  13. return (int)DBStatusEnum.Success;  
  14. }  
  15. else  
  16. {  
  17. return (int)DBStatusEnum.NoResult;  
  18. }  
  19. }  
  20. with UserRepository that has UpdateUser as  
  21. [LogException]  
  22. public int UpdateUser([Required]User user)  
  23. {var parameters = ConstructParamsForEntity(user, "Password""CreateDate""UserInfoId""Activated""Role""ProfileImage""SelectedLocations").ToList();  
  24. if (user.Password != null)  
  25. {  
  26. var parameter = new SqlParameter("PasswordHash", PasswordManager.GetHashString(user.Password));  
  27. parameters.Add(parameter);  
  28. }  
  29. ExecuteNonQuery(GlobalConstants.SP_USER_UPDATE, CommandType.StoredProcedure, parameters.ToArray());  
  30. return (int)DBStatusEnum.Success;  
  31. }  
How can I create patch method in my UserController, I'm not that experienced and not sure how to call it from UserBussinessObject that gets it from UserRepository.cs
 
Here's my patch method so far, so If you get my problem, please help..
  1. [Route("user/{id}"), HttpPatch]  
  2. public IHttpActionResult EditUser(User userUpdate, User user)  
  3. {var userToUpdate = UserBusinessObject.GetUserById(userUpdate.UserId);  
  4. //returns user from user repository?  
  5. //if Id is not found, return  
  6. if (userToUpdate == null)  
  7. return BadRequest("User not found");}  
  8. else return Ok();  
  9. ///call from UserRepository.cs UpdateUser than pass userToUpdate ?  
  10. }  
Thanks a lot, hopefully you'll understand

Answers (1)