Guest User

Untitled

a guest
Jan 29th, 2025
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. using System.Diagnostics;
  2. using System.Security.Claims;
  3. using Microsoft.AspNetCore.Mvc;
  4. using WebApplication1.Models;
  5. using Microsoft.AspNetCore.Authorization;
  6. using WebApplication1.Data;
  7. using Microsoft.AspNetCore.Http;
  8. using Microsoft.AspNetCore.Mvc.Rendering;
  9. using Microsoft.EntityFrameworkCore;
  10. using Microsoft.EntityFrameworkCore.Query;
  11.  
  12. namespace WebApplication1.Controllers;
  13.  
  14. public interface IListingProjectsDtoRepository
  15. {
  16. ListingProjectsDTO getListingProjectsDto(int? Id);
  17. IEnumerable<ListingProjectsDTO> GetAllEmployee();
  18. //Employee Add(Employee employee);
  19. //Employee Update(Employee employeeChanges);
  20. //Employee Delete(int Id);
  21. }
  22.  
  23.  
  24. public class HomeController : Controller
  25. {
  26. private readonly HttpClient _httpClient;
  27. private readonly ApplicationDbContext _context;
  28. private ListingProjectsDTO _listingProjectsDto;
  29. private readonly IListingProjectsDtoRepository _listingProjectsDtoRepository;
  30.  
  31. public HomeController(HttpClient httpClient, ApplicationDbContext context, IListingProjectsDtoRepository listingProjectsDtoRepository)
  32. {
  33. _httpClient = httpClient;
  34. _context = context;
  35. _listingProjectsDtoRepository = listingProjectsDtoRepository;
  36. }
  37.  
  38. [Authorize]
  39. public IActionResult Index()
  40. {
  41. var portalUserModelRef = _context.UsersDBTable
  42. .Select(u => new WebApplication1.Models.PortalUsers
  43. {
  44. Name = u.Name,
  45. Age = u.Age,
  46. Location = u.Location
  47. }).ToList();
  48.  
  49. return View(portalUserModelRef);
  50. }
  51.  
  52. [HttpGet]
  53. public IActionResult EditListingJ(int? Id) //For getting details of the selected User
  54. {
  55. try
  56. {
  57. var ListingDTO_DBTable = _context.ListingDTO_DBTable.Find(Id);
  58. if (ListingDTO_DBTable != null)
  59. {
  60. ListingProjectsDTO dtoModelEdit = new ListingProjectsDTO()
  61. {
  62. Id = ListingDTO_DBTable.Id,
  63. ListingName = ListingDTO_DBTable.ListingName
  64. };
  65. Console.WriteLine($"Got it");
  66. return Json(dtoModelEdit);
  67. }
  68.  
  69. return Json(null);
  70.  
  71. }
  72. catch (Exception ex)
  73. {
  74. Console.WriteLine("so null nothing can be edited");
  75. return null;
  76. }
  77. }
  78.  
  79. [HttpPost]
  80. public IActionResult EditListingJ(ListingProjectsDTO editedDtoModel) //For Updating changes
  81. {
  82. if (ModelState.IsValid)
  83. {
  84. //fetching the user with Id
  85. ListingProjectsDTO _listingProjectsDtoEdit = _listingProjectsDtoRepository.getListingProjectsDto(editedDtoModel.Id);
  86. _listingProjectsDtoEdit.Id = editedDtoModel.Id;
  87. _listingProjectsDtoEdit.ListingName = editedDtoModel.ListingName;
  88. _context.ListingDTO_DBTable.Update(_listingProjectsDto);
  89. _context.SaveChanges();
  90. return Json(_listingProjectsDto);
  91.  
  92.  
  93. }
  94. return Json(null);
  95. }
  96.  
  97. [HttpPost]
  98. public JsonResult CreateUser() //objUser is object which should be same as in javascript function
  99. {
  100. try
  101. {
  102.  
  103. _context.ListingDTO_DBTable.Add(_listingProjectsDto);
  104. _context.SaveChanges();
  105. return Json(_listingProjectsDto); //returning user to javacript
  106.  
  107. }
  108. catch (Exception ex)
  109. {
  110. return null;
  111. }
  112. }
  113.  
  114. public IActionResult Privacy()
  115. {
  116. return View();
  117. }
  118.  
  119.  
  120. [HttpPost]
  121. public JsonResult DeleteUserJ(int Id)
  122. {
  123. try
  124. {
  125. var ListingDTO_DBTable = _context.ListingDTO_DBTable.Find(Id); //fetching the user with Id
  126. if (ListingDTO_DBTable != null)
  127. {
  128. _context.ListingDTO_DBTable.Remove(ListingDTO_DBTable); //deleting from db
  129. _context.SaveChanges();
  130. return Json(ListingDTO_DBTable);
  131. }
  132. return Json(null);
  133. }
  134.  
  135. catch (Exception ex)
  136. {
  137. return null;
  138. }
  139. }
  140.  
  141. [AllowAnonymous]
  142.  
  143. public async Task<IActionResult> TestDashboard1()
  144. {
  145. var datasource = _context.ListingDTO_DBTable.AsQueryable();
  146. var query = datasource
  147. .Select(x => new ListingProjectsDTO() {
  148. Id = x.Id,
  149. ListingName = x.ListingName,
  150. });
  151. var listings = await query.ToListAsync();
  152.  
  153. return View(listings);
  154. }
  155. [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
  156. public IActionResult Error()
  157. {
  158. return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
  159. }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment