Ninad Shinde

Ninad Shinde

  • NA
  • 13
  • 4.6k

Multiple post Method in single API Controller

Feb 7 2019 11:29 PM
Dear all,
 
I am Begineer in MVC 5 and Angular 6 in which I am developing one web application.
There is one angular component(web page) - User.html and User.ts where I am trying to save data  in two tables separately i.e. and .
 
On angular component(web page) i.e. User.html and User.ts, I have separate button to save User header data in table and there is button which opens popup window to save User details data in table.
 
I am passing data from Angular component to APIcontroller in MVC 5(UserController.cs).
On UserController.cs, I have two POST methods i.e. one for Header and another for Details
 
UserController.cs
  1. using DomainDb.Models;  
  2. using DomainDb.Repository;  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Http;  
  8. using System.Web.Http.Cors;  
  9. using System.Web.Mvc;  
  10.   
  11. namespace CURDApis.Controllers  
  12. {  
  13.     /// This is controller class which will be used to expose endpoints.  
  14.     [EnableCors(origins: "*", headers: "*", methods: "*")]  
  15.     public class UserController : ApiController  
  16.     {  
  17.         /// User repository which will be used to write actual business logic.  
  18.         private IUserRepository _userRepository;  
  19.   
  20.         public UserController()  
  21.         {  
  22.             // we can inject repository from unity container.  
  23.             _userRepository = UnityFactory.ResolveObject();  
  24.         }  
  25.   
  26.         //Getting Users Rights for CORS  
  27.         public List GetRightsList(string srights, string srights1)  
  28.         {  
  29.             return _userRepository.GetRightsList(srights, srights1);  
  30.         }  
  31.   
  32.         // GET All User  
  33.         public List Get()  
  34.         {  
  35.             return _userRepository.Get();  
  36.         }  
  37.   
  38.         //Get particular User on the basis of id  
  39.         public User Get(int id)  
  40.         {  
  41.             return _userRepository.Get(id);  
  42.         }  
  43.   
  44.   
  45.         //Deactivates User on the basis of id  
  46.         public void Delete(int id)  
  47.         {  
  48.             _userRepository.Delete(id);  
  49.         }  
  50.   
  51.         //This method will insert or update User component data in  table  
  52.         public void Post([FromBody]User value)  
  53.         {  
  54.             _userRepository.Post(value);  
  55.         }  
  56.   
  57.         //This method will insert or update User component data on popup window to  table  
  58.         public void post([FromBody]User svalue, string suser)  
  59.         {  
  60.             _userRepository.Post(svalue, suser);  
  61.         }  
  62.     }  

Code of public void Post([FromBody]User value) written in a REPOSITORY called as UserRepository.cs - code for inserting or updating data in table
  1. ///   
  2.         ///Inserting or Updating User header data  
  3.         ///  object of user.   
  4.         ///   
  5.         public void Post(User objUser)  
  6.         {  
  7.             GetAuthentications objGetAuthenication = new GetAuthentications();  
  8.             objGetAuthenication.Add = objUser.Add;  
  9.             objGetAuthenication.Modify = objUser.Modify;  
  10.             objGetAuthenication.Deactivate = objUser.Deactivate;  
  11.             objGetAuthenication.View = objUser.View;  
  12.             string auth = objGetAuthenication.GetAuthentication(objGetAuthenication);  
  13.             string InsertQuery = objUser.UserID == 0 ?  
  14.                 @"INSERT IGNORE INTO mstuserheader(UserID,UserName, UserPassword,AuthenticationType,UserRightsID,Activate)  
  15.                 VALUES (?UserID,?UserName, ?UserPassword, ?AuthenticationType,?UserRightsID,?Activate );  
  16.                 SELECT LAST_INSERT_ID();" :
  17. @"UPDATE mstuserheader SET UserName = ?UserName,UserPassword = ?UserPassword,AuthenticationType = ?AuthenticationType,UserRightsID = ?UserRightsID,Activate = ?Activate WHERE UserID = " + objUser.UserID;  
  18.             using (MySqlConnection connEpion = new MySqlConnection(ConnStr))  
  19.             {  
  20.                 connEpion.Open();  
  21.                 int ID = objUser.UserID;  
  22.                 using (MySqlCommand objcmd = new MySqlCommand(InsertQuery))  
  23.                 {  
  24.                     objcmd.Connection = connEpion;  
  25.                     objcmd.Parameters.AddWithValue("UserID", objUser.UserID);                     
  26.                     objcmd.Parameters.AddWithValue("AuthenticationType", auth);  
  27.                     objcmd.Parameters.AddWithValue("UserRightsID", objUser.UserRightsID);  
  28.                     objcmd.Parameters.AddWithValue("Activate", objUser.Activate);  
  29.                     int i = objcmd.ExecuteNonQuery();  
  30.                 }  
  31.                 connEpion.Close();  
  32.             }  
  33.         } 
Code of public void post([FromBody]User svalue, string suser) written in a REPOSITORY called as UserRepository.cs - code for inserting or updating data in table
  1. public void Post(User objUserRights,string suser)  
  2.         {  
  3.             GetAuthentications objGetAuth = new GetAuthentications();  
  4.             objGetAuth.Add = objUserRights.Add;  
  5.             objGetAuth.Modify = objUserRights.Modify;  
  6.             objGetAuth.Deactivate = objUserRights.Deactivate;  
  7.             objGetAuth.View = objUserRights.View;  
  8.   
  9.             string auth = objGetAuth.GetAuthentication(objGetAuth);  
  10.             string query = string.Empty;  
  11.   
  12.             using (MySqlConnection connEpion = new MySqlConnection(ConnStr))  
  13.             {  
  14.                 connEpion.Open();  
  15.                 // checking whether userRightsID and ComponentID is exist or not  
  16.                 using (MySqlCommand objcmd = new MySqlCommand("select count(*) from mstuserrightsdetails where UserRightsID = " + objUserRights.UserRightsID + " and ComponentID = " + objUserRights.ComponentID))  
  17.                 {  
  18.                     objcmd.Connection = connEpion;  
  19.                     //converting result into integer which come as result using ExecuteScalar()  
  20.                     int i = Convert.ToInt32(objcmd.ExecuteScalar());  
  21.                     if (i == 0)  
  22.                     {  
  23.                         // Performing Insert here when it is not exist  
  24.                         query = @"INSERT IGNORE INTO mstuserrightsdetails(UserRightsID,ComponentID,AuthenticationType,Activate)  
  25.                                 VALUES (?UserRightsID,?ComponentID, ?AuthenticationType,?Activate );";  
  26.                     }  
  27.                     else  
  28.                     {  
  29.                         //Performing Update here when it is exist  
  30.                         query = @"UPDATE mstuserrightsdetails SET AuthenticationType = ?AuthenticationType,  
  31.                                 Activate = ?Activate WHERE UserRightsID = " + objUserRights.UserRightsID + " and ComponentID = " + objUserRights.ComponentID;  
  32.                     }  
  33.                     objcmd.CommandText = query;  
  34.                     objcmd.Parameters.AddWithValue("UserRightsID", objUserRights.UserRightsID);  
  35.                     objcmd.Parameters.AddWithValue("ComponentID", objUserRights.ComponentID);  
  36.                     objUserRights.AuthenticationType = auth;  
  37.                     objcmd.Parameters.AddWithValue("AuthenticationType", objUserRights.AuthenticationType);  
  38.                     objcmd.Parameters.AddWithValue("Activate", objUserRights.Activate);  
  39.                     i = objcmd.ExecuteNonQuery();  
  40.                 }  
  41.                 connEpion.Close();  
  42.             }  
  43.         }  
When I am trying to call second post method(public void Post(User objUserRights,string suser)) for saving UserDetails data then I am getting default values for User class and it gets stored into table. here is code for User.cs
 
here is code for User.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace DomainDb.Models  
  8. {  
  9.     //Class of User contains local vaiables for database fields   
  10.     public class User  
  11.     {  
  12.         public int UserID { getset; }  
  13.         public int UserDetailsID { getset; } 
            public int ComponentID { getset; } 
  14.         public string UserName { getset; }  
  15.         public string UserPassword { getset; }  
  16.         public bool Add { getset; }  
  17.         public bool Modify { getset; }  
  18.         public bool Deactivate { getset; }  
  19.   
  20.     }  
  21. }

Answers (1)