hohohotrucken23490

Untitled

Oct 7th, 2024
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. /* View - cshtml */
  2. @{
  3. ViewData["Title"] = "Dashboard";
  4. }
  5.  
  6. <!DOCTYPE html>
  7.  
  8. <html>
  9. <head>
  10. <meta name="viewport" content="width=device-width" />
  11. <title>TestDashboard1</title>
  12. </head>
  13. <body>
  14. <script>
  15. async function fetchData() {
  16. try {
  17. const response = await fetch('/api/APIController/availableListings.json'); //I keep getting 404 not found here!!
  18. if (!response.ok) {
  19. throw new Error(`Error fetching data: ${response.status}`);
  20. }
  21. const data = await response.json();
  22. displayData(data);
  23. } catch (error) {
  24. console.error('Error:', error);
  25. // Handle errors here, like displaying an error message to the user
  26. }
  27. }
  28.  
  29. function displayData(data) {
  30. const listingsContainer = document.getElementById('listings-container'); // Replace with your container element ID
  31. listingsContainer.innerHTML = ''; // Clear any existing content
  32.  
  33. data.forEach(listing => {
  34. const listingElement = document.createElement('div');
  35. listingElement.classList.add('listing'); // Add CSS class for styling
  36.  
  37. const listingName = document.createElement('h3');
  38. listingName.textContent = listing.ListingName;
  39. listingElement.appendChild(listingName);
  40.  
  41. const imageUrl = document.createElement('img');
  42. imageUrl.src = listing.ImageUrl; // Check if ImageUrl has a valid value
  43. listingElement.appendChild(imageUrl);
  44.  
  45. // Add elements for Category and Location details if needed
  46. const category = document.createElement('p');
  47. category.textContent = `Category: ${listing.Category?.propertyName} - ${listing.Category?.propertyType}`; // Check for null values
  48. listingElement.appendChild(category);
  49.  
  50. const location = document.createElement('p');
  51. location.textContent = `Location: ${listing.Location?.City}, ${listing.Location?.State}, ${listing.Location?.PLZ}`; // Check for null values
  52. listingElement.appendChild(location);
  53.  
  54. listingsContainer.appendChild(listingElement);
  55. });
  56. }
  57.  
  58. fetchData();
  59. </script>
  60. </body>
  61. </html>
  62.  
  63. /*APIController.cs */
  64. using Microsoft.AspNetCore.Mvc;
  65. using Microsoft.EntityFrameworkCore;
  66. using WebApplication1.Data;
  67. using WebApplication1.Models;
  68. namespace WebApplication1.Controllers;
  69.  
  70. [Route("api/[controller]")]
  71. [ApiController]
  72. public class APIController : Controller
  73. {
  74.  
  75. private readonly ApplicationDbContext _context;
  76.  
  77. public APIController(ApplicationDbContext context)
  78. {
  79. _context = context;
  80. }
  81.  
  82. [HttpGet("availableListings")] //reference to the availableListings.json
  83. public async Task<IEnumerable<ListingProjects>> Get()
  84. {
  85. return await _context.ListingDBTable.ToListAsync();
  86. }
  87.  
  88. [HttpGet("{id}")]
  89. public async Task<IActionResult> Get(int id)
  90. {
  91. if (id < 1)
  92. return BadRequest();
  93. var product = await _context.ListingDBTable.FirstOrDefaultAsync(m => m.Id == id);
  94. if (product == null)
  95. return NotFound();
  96. return Ok(product);
  97.  
  98. }
  99.  
  100. [HttpPost]
  101. public async Task<IActionResult> Post(ListingProjects listing)
  102. {
  103. _context.Add(listing);
  104. await _context.SaveChangesAsync();
  105. return Ok();
  106. }
  107.  
  108. [HttpPut]
  109. public async Task<IActionResult> Put(ListingProjects listingData)
  110. {
  111. if (listingData == null || listingData.Id == 0)
  112. return BadRequest();
  113.  
  114. var listingTask = await _context.ListingDBTable.FindAsync(listingData.Id);
  115. if (listingTask == null)
  116. return NotFound();
  117. listingTask.ListingName = listingData.ListingName;
  118. listingTask.ImageUrl = listingData.ImageUrl;
  119. listingTask.Category = listingData.Category;
  120. listingTask.Location = listingData.Location;
  121. await _context.SaveChangesAsync();
  122. return Ok();
  123. }
  124.  
  125. [HttpDelete("{id}")]
  126. public async Task<IActionResult> Delete(int id)
  127. {
  128. if (id < 1)
  129. return BadRequest();
  130. var listingDel = await _context.ListingDBTable.FindAsync(id);
  131. if (listingDel == null)
  132. return NotFound();
  133. _context.ListingDBTable.Remove(listingDel);
  134. await _context.SaveChangesAsync();
  135. return Ok();
  136.  
  137. }
  138.  
  139.  
  140. }
  141.  
  142. /*availableListings.json */
  143. {
  144. "properties": [
  145. {
  146. "property_name": "St Johannes 3",
  147. "property_type": "2er WG",
  148. "rent_price_monthly": 600,
  149. "ad_published_date": "2024-09-15"
  150. },
  151. {
  152. "property_name": "St Sebald A",
  153. "property_type": "1-Zimmer-Wohnung",
  154. "rent_price_monthly": 1000,
  155. "ad_published_date": "2024-09-15"
  156. },
  157.  
Advertisement
Add Comment
Please, Sign In to add comment