public IActionResult Put(int id, [FromBody] string value) { var studentChangeInput = JsonConvert.DeserializeObject <dynamic>(value); string fullName = studentChangeInput.FullName.Value; string email = studentChangeInput.Email.Value; string admissionId = studentChangeInput.AdmissionId.Value; string mobileContact = studentChangeInput.MobileContact.Value; //DateTime datatype is not that straightforward. //Need to make a DateTime datatype conversion first because //the DateOfBirth of the foundOneStudent instance is DateTime datatype. DateTime dateOfBirth = DateTime.ParseExact(studentChangeInput.DateOfBirth.Value, "d/M/yyyy", CultureInfo.InvariantCulture); string courseId = studentChangeInput.CourseId.Value; var successRequestResultMessage = new { Message = string.Format("Server side has received the data. For example, full name is {0} email is {1} courseId is {2}. The logic in this Put() method are all dummy code. You will need to do it in the subsequent practical exerices.", fullName, email, courseId) }; //Create a HttpOkObjectResult class instance, httpOkResult. //When creating the object, provide the previous message object into it. HttpOkObjectResult httpOkResult = new HttpOkObjectResult(successRequestResultMessage); //Send the HttpOkObjectResult class object back to the client. return(httpOkResult); }
public IActionResult Put(int id, [FromBody] string value) { string customMessage = ""; var companyChangeInput = JsonConvert.DeserializeObject <dynamic>(value); //To obtain the company name information: //use companyChangeInput.CompanyName.value //To obtain the address information: //use companyChangeInput.Address.value try { //Find the Company Entity through the Companies Entity Set //by calling the Single() method. //I learnt Single() method from this online reference: //http://geekswithblogs.net/BlackRabbitCoder/archive/2011/04/14/c.net-little-wonders-first-and-single---similar-yet-different.aspx var foundOneCompany = Database.Companies .Single(item => item.CompanyId == id); foundOneCompany.CompanyName = companyChangeInput.CompanyName; foundOneCompany.Address = companyChangeInput.Address.Value; foundOneCompany.PostalCode = companyChangeInput.PostalCode.Value; foundOneCompany.CompanyTypeId = Int32.Parse(companyChangeInput.CompanyTypeId.Value); foundOneCompany.UpdatedAt = DateTime.Now; //Tell the database model to commit/persist the changes to the database, //I use the following command. Database.SaveChanges(); } catch (Exception ex) { if (ex.InnerException.Message .Contains("Company_CompanyName_UniqueConstraint") == true) { customMessage = "Unable to save company record due " + "to another record having the same name as : " + companyChangeInput.CompanyName.Value; //Create a fail message anonymous object that has one property, Message. //This anonymous object's Message property contains a simple string message object httpFailRequestResultMessage = new { Message = customMessage }; //Return a bad http request message to the client return(HttpBadRequest(httpFailRequestResultMessage)); } }//End of try .. catch block on saving data //Construct a custom message for the client //Create a success message anonymous object which has a //Message member variable (property) var successRequestResultMessage = new { Message = "Saved company record" }; //Create a HttpOkObjectResult class instance, httpOkResult. //When creating the object, provide the previous message object into it. HttpOkObjectResult httpOkResult = new HttpOkObjectResult(successRequestResultMessage); //Send the HttpOkObjectResult class object back to the client. return(httpOkResult); }//End of Put() Web API method (// PUT api/Companies/5)
public IActionResult Post([FromBody] string value) { string customMessage = ""; Student oneNewStudent = new Student(); var studentNewInput = JsonConvert.DeserializeObject <dynamic>(value); //To obtain the full name information, use studentNewInput.FullName.value //To obtain the email information, use studentNewInput.Email.value oneNewStudent.FullName = studentNewInput.FullName.Value; oneNewStudent.Email = studentNewInput.Email.Value; oneNewStudent.AdmissionId = studentNewInput.AdmissionId.Value; oneNewStudent.MobileContact = studentNewInput.MobileContact.Value; //DateTime datatype is not that straightforward. //Need to make a DateTime datatype conversion first because //the DateOfBirth of the foundOneStudent instance is DateTime datatype. oneNewStudent.DateOfBirth = DateTime.ParseExact(studentNewInput.DateOfBirth.Value, "d/M/yyyy", CultureInfo.InvariantCulture); oneNewStudent.CourseId = Convert.ToInt32(studentNewInput.CourseId.Value); try { Database.Students.Add(oneNewStudent); Database.SaveChanges(); } catch (Exception ex) { if (ex.InnerException.Message .Contains("Student_AdmissionId_UniqueConstraint") == true) { customMessage = "Unable to save student record due " + "to another record having the same admin id : " + studentNewInput.AdmissionId.Value; //Create a fail message anonymous object that has one property, Message. //This anonymous object's Message property contains a simple string message object httpFailRequestResultMessage = new { Message = customMessage }; //Return a bad http request message to the client return(HttpBadRequest(httpFailRequestResultMessage)); } }//End of try .. catch block on saving data //Construct a custom message for the client //Create a success message anonymous object which has a //Message member variable (property) var successRequestResultMessage = new { Message = "Saved student record" }; //Create a HttpOkObjectResult class instance, httpOkResult. //When creating the object, provide the previous message object into it. HttpOkObjectResult httpOkResult = new HttpOkObjectResult(successRequestResultMessage); //Send the HttpOkObjectResult class object back to the client. return(httpOkResult); }//End of Post() method
public IActionResult Post([FromBody] string value) { string customMessage = ""; //Reconstruct a useful object from the input string value. dynamic companyNewInput = JsonConvert.DeserializeObject <dynamic>(value); Company newCompany = new Company(); try { //Copy out all the company data into the new Company instance, //newCompany. newCompany.CompanyName = companyNewInput.CompanyName.Value; newCompany.Address = companyNewInput.Address.Value; newCompany.PostalCode = companyNewInput.PostalCode.Value; newCompany.CompanyTypeId = Int32.Parse(companyNewInput.CompanyTypeId.Value); Database.Companies.Add(newCompany); Database.SaveChanges(); //Telling the database model to save the changes } catch (Exception exceptionObject) { if (exceptionObject.InnerException.Message .Contains("Company_CompanyName_UniqueConstraint") == true) { customMessage = "Unable to save company record due " + "to another record having the same name as : " + companyNewInput.CompanyName.Value; //Create a fail message anonymous object that has one property, Message. //This anonymous object's Message property contains a simple string message object httpFailRequestResultMessage = new { Message = customMessage }; //Return a bad http request message to the client return(HttpBadRequest(httpFailRequestResultMessage)); } } //End of Try..Catch block //If there is no runtime error in the try catch block, the code execution //should reach here. Sending success message back to the client. //****************************************************** //Construct a custom message for the client //Create a success message anonymous object which has a //Message member variable (property) var successRequestResultMessage = new { Message = "Saved company record" }; //Create a HttpOkObjectResult class instance, httpOkResult. //When creating the object, provide the previous message object into it. HttpOkObjectResult httpOkResult = new HttpOkObjectResult(successRequestResultMessage); //Send the HttpOkObjectResult class object back to the client. return(httpOkResult); } //End of Post() method
public IActionResult Post([FromBody] string value) { string customMessage = ""; //Reconstruct a useful object from the input string value. dynamic courseNewInput = JsonConvert.DeserializeObject <dynamic>(value); Course newCourse = new Course(); try { //Copy out all the course data into the new Course instance, //new. newCourse.CourseAbbreviation = courseNewInput.CourseAbbreviation.Value; newCourse.CourseName = courseNewInput.CourseName.Value; //When I add this Course instance, newCourse into the //Courses Entity Set, it will turn into a Course entity waiting to be mapped //as a new record inside the actual Course table. Database.Courses.Add(newCourse); Database.SaveChanges();//Telling the database model to save the changes } catch (Exception exceptionObject) { if (exceptionObject.InnerException.Message .Contains("Course_CourseAbbreviation_UniqueConstraint") == true) { customMessage = "Unable to save course record due " + "to another record having the same abbreviation : " + courseNewInput.CourseAbbreviation.Value; //Create an anonymous object that has one property, Message. //This anonymous object's Message property contains a simple string message object httpFailRequestResultMessage = new { Message = customMessage }; //Return a bad http request message to the client return(HttpBadRequest(httpFailRequestResultMessage)); } }//End of Try..Catch block //If there is no runtime error in the try catch block, the code execution //should reach here. Sending success message back to the client. //****************************************************** //Construct a custom message for the client //Create a success message anonymous object which has a //Message member variable (property) var successRequestResultMessage = new { Message = "Saved course record" }; //Create a HttpOkObjectResult class instance, httpOkResult. //When creating the object, provide the previous message object into it. HttpOkObjectResult httpOkResult = new HttpOkObjectResult(successRequestResultMessage); //Send the HttpOkObjectResult class object back to the client. return(httpOkResult); }//End of POST api
public IActionResult Put(int id, [FromBody] string value) { string customMessage = ""; var studentChangeInput = JsonConvert.DeserializeObject <dynamic>(value); // var foundOneStudent = Database.Students .Where(item => item.StudentId == id).FirstOrDefault(); foundOneStudent.FullName = studentChangeInput.FullName.Value; foundOneStudent.Email = studentChangeInput.Email.Value; foundOneStudent.MobileContact = studentChangeInput.MobileContact.Value; foundOneStudent.AdmissionId = studentChangeInput.AdmissionId.Value; foundOneStudent.DateOfBirth = DateTime.ParseExact(studentChangeInput.DateOfBirth.Value, "d/M/yyyy", CultureInfo.InvariantCulture); foundOneStudent.CourseId = Convert.ToInt32(studentChangeInput.CourseId.Value); foundOneStudent.UpdatedAt = DateTime.Now; try { Database.Update(foundOneStudent); Database.SaveChanges(); } catch (Exception ex) { if (ex.InnerException.Message .Contains("Student_AdmissionId_UniqueConstraint") == true) { customMessage = "Unable to save student record due " + "to another record having the same admin id: " + studentChangeInput.AdmissionId.Value; //Create a fail message anonymous object that has one property, Message. //This anonymous object's Message property contains a simple string message object HttpFailRequestResultMessage = new { Message = customMessage }; //Return a bad http request message to the client return(HttpBadRequest(HttpFailRequestResultMessage)); } } //End of try .. catch block on saving data //Construct a custom message for the client //Create a success message anonymous object which has a //Message member variable (property) var successRequestResultMessage = new { Message = "Saved student record" }; //Create a HttpOkObjectResult class instance, httpOkResult. //When creating the object, provide the previous message object into it HttpOkObjectResult httpOkResult = new HttpOkObjectResult(successRequestResultMessage); //Send the HttpOkObjectResult class object back to the client return(httpOkResult); }//End of Put() method
public IActionResult Put(int id, [FromBody] string value) { string customMessage = ""; var courseChangeInput = JsonConvert.DeserializeObject <dynamic>(value); //After reconstructing the object from the JSON string residing in the input parameter variable, //value: //To obtain the course Abbreviation information, use courseChangeInput.CourseAbbreviation.Value //To obtain the course name information, use courseChangeInput.CourseName.Value var oneCourse = Database.Courses .Where(courseEntity => courseEntity.CourseId == id).Single(); oneCourse.CourseAbbreviation = courseChangeInput.CourseAbbreviation.Value; oneCourse.CourseName = courseChangeInput.CourseName.Value; oneCourse.UpdatedAt = DateTime.Now; try { Database.SaveChanges(); } catch (Exception ex) { if (ex.InnerException.Message .Contains("Course_CourseAbbreviation_UniqueConstraint") == true) { customMessage = "Unable to save course record due " + "to another record having the same name as : " + courseChangeInput.CourseAbbreviation.Value; //Create an anonymous object that has one property, Message. //This anonymous object's Message property contains a simple string message object httpFailRequestResultMessage = new { Message = customMessage }; //Return a bad http request message to the client return(HttpBadRequest(httpFailRequestResultMessage)); } }//End of try .. catch block on saving data //Construct a custom message for the client //Create a success message anonymous object which has a //Message member variable (property) var successRequestResultMessage = new { Message = "Saved course record" }; //Create a HttpOkObjectResult class instance, httpOkResult. //When creating the object, provide the previous message object into it. HttpOkObjectResult httpOkResult = new HttpOkObjectResult(successRequestResultMessage); //Send the HttpOkObjectResult class object back to the client. return(httpOkResult); }//End of Put() Web API method
public IActionResult Delete(int id) { //Build a custom message for the client //Create a success message anonymous object which has a //Message member variable (property) var successRequestResultMessage = new { Message = string.Format("Server side Delete() Web API method of Students Web API controller object has received the id {0} which you want to delete. You need to code the Delete() method in subsequent practical exericises", id.ToString()) }; //Create a HttpOkObjectResult class instance, httpOkResult. //When creating the object, provide the previous message object into it. HttpOkObjectResult httpOkResult = new HttpOkObjectResult(successRequestResultMessage); //Send the HttpOkObjectResult class object back to the client. return(httpOkResult); }//end of Delete() Web API method with /apis/students/digit route
public IActionResult Delete(int id) { string customMessage = ""; //The following command should not be used. Although can work too. // var existingOneCompany = Database.Companies // .Where(item => item.CompanyId == id).FirstOrDefault(); //-------------------------------------------------------------------------------------------- try { var foundOneCompany = Database.Companies .Single(item => item.CompanyId == id); foundOneCompany.DeletedAt = DateTime.Now; //Update the database model Database.Update(foundOneCompany); //Tell the db model to commit/persist the changes to the database, //I use the following command. Database.SaveChanges(); } catch (Exception ex) { customMessage = "Unable to delete company record."; object httpFailRequestResultMessage = new { Message = customMessage }; //Return a bad http request message to the client return(HttpBadRequest(httpFailRequestResultMessage)); }//End of try .. catch block on manage data //Build a custom message for the client //Create a success message anonymous object which has a //Message member variable (property) var successRequestResultMessage = new { Message = "Deleted company record" }; //Create a HttpOkObjectResult class instance, httpOkResult. //When creating the object, provide the previous message object into it. HttpOkObjectResult httpOkResult = new HttpOkObjectResult(successRequestResultMessage); //Send the HttpOkObjectResult class object back to the client. return(httpOkResult); }//end of Delete() Web API method with /apis/values/digit route
public IActionResult Delete(int id) { string customMessage = ""; try { var foundOneStudent = Database.Students .Single(eachStudent => eachStudent.StudentId == id); //Update the DeletedAt property with the current date and time foundOneStudent.DeletedAt = DateTime.Now; //Update the database model Database.Update(foundOneStudent); //Tell the db model to commit/persist the changes to the database. Database.SaveChanges(); } catch (Exception ex) { customMessage = "Unable to delete student record."; object httpFailRequestResultMessage = new { Message = customMessage }; //Return a bad http request message to the client return(HttpBadRequest(httpFailRequestResultMessage)); }//End of try..catch block on manage data //Build a custom message for the client //Create a success message anonymous object which has a //Message member variable (property) var successRequestResultMessage = new { Message = "Deleted student record" }; //Create a HttpOkObjectResult class instance, httpOkResult. //When creating the object, provide the prvious message object into it. HttpOkObjectResult httpOkResult = new HttpOkObjectResult(successRequestResultMessage); //Send the HttpOkObjectResult class object back to the client. return(httpOkResult); }//end of Delete() Web API method with /api/students/digit route
public IActionResult Delete(int id) { string customMessage = ""; try { var foundOneCompanyType = Database.CompanyTypes .Single(eachCompanyType => eachCompanyType.CompanyTypeId == id); foundOneCompanyType.DeletedAt = DateTime.Now; //Tell the db model to commit/persist the changes to the database, //I use the following command. Database.SaveChanges(); } catch (Exception ex) { customMessage = "Unable to delete company type record."; object httpFailRequestResultMessage = new { Message = customMessage }; //Return a bad http request message to the client return(HttpBadRequest(httpFailRequestResultMessage)); }//End of try .. catch block on manage data //Build a custom message for the client //Create a success message anonymous object which has a //Message member variable (property) var successRequestResultMessage = new { Message = "Deleted company type record" }; //Create a HttpOkObjectResult class instance, httpOkResult. //When creating the object, provide the previous message object into it. HttpOkObjectResult httpOkResult = new HttpOkObjectResult(successRequestResultMessage); //Send the HttpOkObjectResult class object back to the client. return(httpOkResult); }//end of Delete() Web API method with /apis/CompanyTypes/<record id>
public async Task <IActionResult> Delete([FromQuery] string value) { string customMessage = ""; var listOfId = value.Split(',').ToList(); try { var companyList = Database.Companies.Where(x => listOfId.Contains(x.CompanyId.ToString())); await companyList.ForEachAsync(a => a.DeletedAt = DateTime.Now); //Update the database model Database.UpdateRange(companyList); //Tell the db model to commit/persist the changes to the database, //I use the following command. Database.SaveChanges(); } catch (Exception ex) { customMessage = "Unable to delete company record(s)."; object httpFailRequestResultMessage = new { Message = customMessage }; //Return a bad http request message to the client return(HttpBadRequest(httpFailRequestResultMessage)); }//End of try .. catch block on saving data //Construct a custom message for the client //Create a success message anonymous object which has a Message member variable (property) var successRequestResultMessage = new { Message = "Deleted company record(s)" }; //Create a HttpOkObjectResult class instance, httpOkResult. //When creating the object, provide the previous message object into it. HttpOkObjectResult httpOkResult = new HttpOkObjectResult(successRequestResultMessage); //Send the HttpOkObjectResult class object back to the client. return(httpOkResult); } //End of bulk delete method
public IActionResult Post([FromBody]string value) { string customMessage = ""; //Reconstruct a useful object from the input string value dynamic companyNewInput = JsonConvert.DeserializeObject<dynamic>(value); Company newCompany = new Company(); try { //Copy out all the company data into the new Company instance, //newCompany. newCompany.CompanyName = companyNewInput.CompanyName.Value; newCompany.Address = companyNewInput.Address.Value; newCompany.PostalCode = companyNewInput.PostalCode.Value; Database.Companies.Add(newCompany); Database.SaveChanges(); } catch (Exception exceptionObject) { if (exceptionObject.InnerException.Message .Contains("Company_CompanyName_UniqueConstraint") == true) { customMessage = "Unable to save company record due" + "to another record having the same name as :" + "companyNewInput.CompanyName.Value"; //Create a fail fail message generic object that has one property, Message. //This generic object's Message property contains a simple string message object httpFailRequestResultMessage = new { Message = customMessage }; //Return a bad http request message to the client return HttpBadRequest(httpFailRequestResultMessage); } } //End of Try catch block //If there is no runtime error in the try catch block, the code execution //should reach here. Sending success message back to the client. //********************************************************************** //Construct a custom message for the client //Create a success message generic object which has a //Message member variable (property) var successRequestResultMessage = new { Message = "Saved company record" }; //Create a HttpOkObjectResult class instance, httpOkResult. //When creating the object, provide the previous message object into it. HttpOkObjectResult httpOkResult = new HttpOkObjectResult(successRequestResultMessage); //Send the HttpOkObjectResult class object back to the client. return httpOkResult; //End of POST api //var company = JsonConvert.DeserializeObject<dynamic>(value); ////To obtain the company name information, use company.CompanyName.value ////To obtain the address information, use company.Address.value ////Create a new Company type instance, oneCompany //Company oneCompany = new Company(); ////Use the following 3 lines to supply user provided values ////into the oneCompany's respective properties //oneCompany.CompanyName = company.CompanyName.Value; //oneCompany.Address = company.Address.Value; //oneCompany.PostalCode = company.PostalCode.Value; ////The object Database represents the database. The Database object has a property, Companies ////which references the Company entity (table) in the database. ////Calling the Add() method to add the filled Company type instance, oneCompany //Database.Companies.Add(oneCompany); //try //{ // //Tell the Database object which references the database to begin committing the // //changes. Calling the SaveChanges() method will persist the new company data // //in the database. // Database.SaveChanges(); //} //catch (DbUpdateException ex) //{ // string messageToUser = ""; // //Code to return the message to the user informing him that he is entering a duplicate company name // if (ex.InnerException.Message.Contains("Company_CompanyName_UniqueConstraint") == true) // { // messageToUser = "******" + // company.CompanyName.Value; // } // var errorResponse = new { Status = "fail", Message = messageToUser }; // return new JsonResult(errorResponse); //} //var successResponse = new { Status = "success", Message = "Saved company record." }; //return new JsonResult(successResponse); }//End of Post API (// POST api/values)