hohohotrucken23490

Untitled

Nov 9th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.EntityFrameworkCore;
  3. using WebApplication1.Data;
  4. using WebApplication1.Models;
  5. namespace WebApplication1.Controllers;
  6.  
  7. [Route("api/[controller]")]
  8. [ApiController]
  9. public class APIController : Controller
  10. {
  11.  
  12. private readonly ApplicationDbContext _context;
  13.  
  14. public APIController(ApplicationDbContext context)
  15. {
  16. _context = context;
  17. }
  18.  
  19. [HttpGet("availableListings")]
  20. public async Task<IEnumerable<ListingProjects>> Get()
  21. {
  22. return await _context.ListingDBTable.ToListAsync();
  23. }
  24.  
  25. [HttpGet("{id}")]
  26. public async Task<IActionResult> Get(int id)
  27. {
  28. if (id < 1)
  29. return BadRequest();
  30. var product = await _context.ListingDBTable.FirstOrDefaultAsync(m => m.Id == id);
  31. if (product == null)
  32. return NotFound();
  33. return Ok(product);
  34.  
  35. }
  36.  
  37. [HttpPost]
  38. public async Task<IActionResult> Post(ListingProjects listing)
  39. {
  40. _context.Add(listing);
  41. await _context.SaveChangesAsync();
  42. return Ok();
  43. }
  44.  
  45. [HttpPut]
  46. public async Task<IActionResult> Put(ListingProjects listingData)
  47. {
  48. if (listingData == null || listingData.Id == 0)
  49. return BadRequest();
  50.  
  51. var listingTask = await _context.ListingDBTable.FindAsync(listingData.Id);
  52. if (listingTask == null)
  53. return NotFound();
  54. listingTask.ListingName = listingData.ListingName;
  55. listingTask.ImageUrl = listingData.ImageUrl;
  56. listingTask.Category = listingData.Category;
  57. listingTask.Location = listingData.Location;
  58. await _context.SaveChangesAsync();
  59. return Ok();
  60. }
  61.  
  62. [HttpDelete("{id}")]
  63. public async Task<IActionResult> Delete(int id)
  64. {
  65. if (id < 1)
  66. return BadRequest();
  67. var listingDel = await _context.ListingDBTable.FindAsync(id);
  68. if (listingDel == null)
  69. return NotFound();
  70. _context.ListingDBTable.Remove(listingDel);
  71. await _context.SaveChangesAsync();
  72. return Ok();
  73.  
  74. }
  75.  
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment