hohohotrucken23490

Untitled

Nov 11th, 2024
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. /*Model*/
  2. using System.Collections;
  3. using Microsoft.Build.Framework;
  4.  
  5. namespace WebApplication1.Models;
  6.  
  7. public class ListingProjects : IEnumerable
  8. {
  9. //public string? inputsToDB { get; set; }
  10. public int Id { get; set; }
  11.  
  12. [Required]
  13. public string? ListingName { get; set; }
  14. public string? ImageUrl { get; set; }
  15.  
  16. //Navigation properties to child model classes
  17. public int? CategoryId { get; set; }
  18. public Category? Category { get; set; }
  19.  
  20. public int? LocationId { get; set; }
  21. public Location? Location { get; set; }
  22. // Implement IEnumerable correctly
  23. public IEnumerator<ListingProjects> GetEnumerator()
  24. {
  25. yield return this;
  26. }
  27.  
  28. IEnumerator IEnumerable.GetEnumerator()
  29. {
  30. return GetEnumerator();
  31. }
  32. }
  33. /*DTO*/
  34. public class ListingProjectsDTO
  35. {
  36. [System.ComponentModel.DataAnnotations.Key]
  37. public int? ListingId { get; set; }
  38. public string? ListingName_DTO { get; set; }
  39. }
  40. /*Insert View*/
  41. @model ListingProjects
  42.  
  43. @{
  44. ViewData["Title"] = "insert listings";
  45. }
  46.  
  47. <!DOCTYPE html>
  48. <html>
  49. <head>
  50. <meta name="viewport" content="width=device-width" />
  51. <title>Index</title>
  52. </head>
  53. <body>
  54.  
  55. @Html.AntiForgeryToken()
  56. @using (Html.BeginForm("InsertListings", "Home", FormMethod.Post))
  57. {
  58. <table id="tblCust" class="table table-bordered">
  59. <thead>
  60. <tr>
  61. <td>@Html.LabelFor(model => model.ListingName, new { @class = "control-label" })</td>
  62. </tr>
  63. <tr>
  64. <td>@Html.TextBoxFor(model => model.ListingName, new { @class = "form-control" })</td>
  65. </tr>
  66. <tr>
  67. <td><input type="submit" style="float:right;" data-entity-id="@Model?.Id" id="saveBtn" class="btn btn-info" value="Save"/></td>
  68. </tr>
  69. </thead>
  70. <tbody></tbody>
  71. </table>
  72. }
  73. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  74. <script>
  75. var listingId = "@Model?.Id";
  76. </script>
  77. <script src="~/js/site.js"></script>
  78. </body>
  79. </html>
  80.  
  81. /* API Controller*/
  82. [Route("api/[controller]")]
  83. [ApiController]
  84. public class APIController : Controller
  85. {
  86.  
  87. private readonly ApplicationDbContext _context;
  88.  
  89. public APIController(ApplicationDbContext context)
  90. {
  91. _context = context;
  92. }
  93.  
  94. [HttpGet("availableListings")]
  95. public async Task<List<ListingProjectsDTO>> GetListings()
  96. {
  97.  
  98. var listings = from b in _context.ListingDTO_DBTable
  99. select new ListingProjectsDTO()
  100. {
  101. ListingId = b.ListingId,
  102. ListingName_DTO = b.ListingName_DTO
  103. };
  104.  
  105. return listings.ToList();
  106.  
  107. }
  108. [ProducesResponseType(typeof(List<ListingProjectsDTO>), 200)]
  109. [HttpGet("{id}")]
  110. public async Task<IActionResult> Get(int id)
  111. {
  112. if (id < 1)
  113. return BadRequest();
  114. var product = await _context.ListingDBTable.FirstOrDefaultAsync(m => m.Id == id);
  115. if (product == null)
  116. return NotFound();
  117. return Ok(product);
  118.  
  119. }
  120.  
  121. [ProducesResponseType(typeof(List<ListingProjectsDTO>), 200)]
  122. [HttpPost]
  123. public async Task<IActionResult> Post(ListingProjects listing)
  124. {
  125. if (!ModelState.IsValid)
  126. {
  127. return BadRequest(ModelState);
  128. }
  129.  
  130. _context.Add(listing);
  131. await _context.SaveChangesAsync();
  132. _context.Entry(listing).Reference(x => x.ListingName).Load();
  133.  
  134. var dto = new ListingProjectsDTO()
  135. {
  136. ListingId = listing.Id,
  137. ListingName_DTO = listing.ListingName
  138.  
  139. };
  140.  
  141. return CreatedAtRoute("DefaultApi", new { id = listing.Id }, dto);
  142. }
  143.  
  144. [HttpPut]
  145. public async Task<IActionResult> Put(ListingProjects listingData)
  146. {
  147. if (listingData == null || listingData.Id == 0)
  148. return BadRequest();
  149.  
  150. var listingTask = await _context.ListingDBTable.FindAsync(listingData.Id);
  151. if (listingTask == null)
  152. return NotFound();
  153. listingTask.ListingName = listingData.ListingName;
  154. listingTask.ImageUrl = listingData.ImageUrl;
  155. listingTask.Category = listingData.Category;
  156. listingTask.Location = listingData.Location;
  157. await _context.SaveChangesAsync();
  158. return Ok();
  159. }
  160.  
  161. [HttpDelete("{id}")]
  162. public async Task<IActionResult> Delete(int id)
  163. {
  164. if (id < 1)
  165. return BadRequest();
  166. var listingDel = await _context.ListingDBTable.FindAsync(id);
  167. if (listingDel == null)
  168. return NotFound();
  169. _context.ListingDBTable.Remove(listingDel);
  170. await _context.SaveChangesAsync();
  171. return Ok();
  172.  
  173. }
  174.  
  175.  
  176. }
  177. /*HomeController, only insert functions listed*/
  178. [HttpGet]
  179. public IActionResult InsertListings()
  180. {
  181. return View();
  182. }
  183.  
  184. [HttpPost]
  185. [ValidateAntiForgeryToken]
  186. public ActionResult InsertListings([Bind("ListingName")] ListingProjects listingProject) // Change to ListingProjects model
  187. {
  188.  
  189. try
  190. {
  191. if (ModelState.IsValid)
  192. {
  193. _context.ListingDBTable.Add(listingProject);
  194. _context.SaveChanges();
  195. return RedirectToAction("TestDashboard1"); // Redirect to dashboard
  196. }
  197. else
  198. {
  199. // Model is invalid, handle it (e.g., show validation errors)
  200. return View(listingProject); // Return the view with error messages
  201. }
  202. }
  203. catch (DbUpdateException dbUpdateException)
  204. {
  205. ModelState.AddModelError("", "Unable to save changes. " +
  206. "Try again, and if the problem persists " +
  207. "see your system administrator.");
  208. }
  209.  
  210. return View(listingProject);
  211. }
  212.  
  213.  
  214.  
Advertisement
Add Comment
Please, Sign In to add comment