Adhikar Patil

Adhikar Patil

  • 1.6k
  • 235
  • 8.8k

How implement GET and PUT API for updating two tables data

Aug 12 2019 10:39 PM
Hello,
 
I have two tables - Student Table and Student address table and this tables having relation one to one and I want to write an Get and Put API for those two tables for fetching and updating data using Asp.Net Core using single ViewModels.
 
Student Table Columns-
StudentId, Name, MobileNo
 
Student Address Table Columns-
StudentId, Address, City.
 
Source Code-
  1. using System;  
  2. using System.Threading.Tasks;  
  3. using Bds.Api.AutoMapper;  
  4. using Bds.Api.Models.Public;  
  5. using Bds.Data.Models;  
  6. using Bds.Service.Helpers;  
  7. using Bds.Service.Interfaces;  
  8. using Microsoft.AspNetCore.Authorization;  
  9. using Microsoft.AspNetCore.Mvc;  
  10. namespace Bds.Api.Controllers.Public.V1  
  11. { [Route("api/v1/[controller]")]  
  12. [Authorize]  
  13. public class CoverController : Controller {  
  14. private readonly IStudentService _studentService;  
  15. private readonly IStudentAddressService _studentaadressService;  
  16. public CoverController(IStudentService studentService, IStudentAddressService studentaadressService) {  
  17. _studentService= studentService;  
  18. _studentaadressService= studentaadressService; }  
  19. [HttpGet("{id}")]  
  20. public async Task<IActionResult> Get(int id) {  
  21. try {  
  22. var studModel = await _studentService.GetSingleByConditionAsync(u => u.StudentId== id);   
  23. if (studModel == nullreturn BadRequest(ResponseMessages.GetErrorMessage(ResponseMessages.MessageType.NotFound));   
  24. return Ok(studModel.ToSingle<StudentModel>());   
  25. var studAddressModel = await _studentaadressService.GetSingleByConditionAsync(u => u.StudentId== id);   
  26. if (studAddressModel == nullreturn BadRequest(ResponseMessages.GetErrorMessage(ResponseMessages.MessageType.NotFound));   
  27. return Ok(studAddressModel.ToSingle<StudentAddressModel>());   
  28. }   
  29. catch (Exception ex)   
  30. {   
  31. return StatusCode(500, ResponseMessages.GetErrorMessage(ResponseMessages.MessageType.Fetch, ex.Message));   
  32. }   
  33. }   
  34. [HttpPut("{id}")]   
  35. public async Task<IActionResult> Put(int id, [FromBody]StudentModel studmodel, [FromBody]StudentAddressModel studaddmodel)   
  36. {   
  37. try {   
  38. if (id == 0 || studmodel== null || studaddmodel==null)   
  39. return BadRequest(ResponseMessages.GetErrorMessage(ResponseMessages.MessageType.InValid)); var response = await _studentService.UpdateAsync(model.ToSingle<Student>());   
  40. var response = await _studentaadressService.UpdateAsync(model.ToSingle<StudentAddress>());   
  41. return response > 0 ? Ok(ResponseMessages.GetSuccessMessage(ResponseMessages.MessageType.Edit)) : StatusCode(500, ResponseMessages.GetErrorMessage(ResponseMessages.MessageType.Edit));   
  42. }   
  43. catch (Exception ex) {   
  44. return StatusCode(500, ResponseMessages.GetErrorMessage(ResponseMessages.MessageType.Edit, ex.Message));   
  45. }   
  46. }   
  47. }  
  48. }

Answers (2)