Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Diagnostics;
- using System.Security.Claims;
- using Microsoft.AspNetCore.Mvc;
- using WebApplication1.Models;
- using Microsoft.AspNetCore.Authorization;
- using WebApplication1.Data;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc.Rendering;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.EntityFrameworkCore.Query;
- namespace WebApplication1.Controllers;
- public interface IListingProjectsDtoRepository
- {
- ListingProjectsDTO getListingProjectsDto(int? Id);
- IEnumerable<ListingProjectsDTO> GetAllEmployee();
- //Employee Add(Employee employee);
- //Employee Update(Employee employeeChanges);
- //Employee Delete(int Id);
- }
- public class HomeController : Controller
- {
- private readonly HttpClient _httpClient;
- private readonly ApplicationDbContext _context;
- private ListingProjectsDTO _listingProjectsDto;
- private readonly IListingProjectsDtoRepository _listingProjectsDtoRepository;
- public HomeController(HttpClient httpClient, ApplicationDbContext context, IListingProjectsDtoRepository listingProjectsDtoRepository)
- {
- _httpClient = httpClient;
- _context = context;
- _listingProjectsDtoRepository = listingProjectsDtoRepository;
- }
- [Authorize]
- public IActionResult Index()
- {
- var portalUserModelRef = _context.UsersDBTable
- .Select(u => new WebApplication1.Models.PortalUsers
- {
- Name = u.Name,
- Age = u.Age,
- Location = u.Location
- }).ToList();
- return View(portalUserModelRef);
- }
- [HttpGet]
- public IActionResult EditListingJ(int? Id) //For getting details of the selected User
- {
- try
- {
- var ListingDTO_DBTable = _context.ListingDTO_DBTable.Find(Id);
- if (ListingDTO_DBTable != null)
- {
- ListingProjectsDTO dtoModelEdit = new ListingProjectsDTO()
- {
- Id = ListingDTO_DBTable.Id,
- ListingName = ListingDTO_DBTable.ListingName
- };
- Console.WriteLine($"Got it");
- return Json(dtoModelEdit);
- }
- return Json(null);
- }
- catch (Exception ex)
- {
- Console.WriteLine("so null nothing can be edited");
- return null;
- }
- }
- [HttpPost]
- public IActionResult EditListingJ(ListingProjectsDTO editedDtoModel) //For Updating changes
- {
- if (ModelState.IsValid)
- {
- //fetching the user with Id
- ListingProjectsDTO _listingProjectsDtoEdit = _listingProjectsDtoRepository.getListingProjectsDto(editedDtoModel.Id);
- _listingProjectsDtoEdit.Id = editedDtoModel.Id;
- _listingProjectsDtoEdit.ListingName = editedDtoModel.ListingName;
- _context.ListingDTO_DBTable.Update(_listingProjectsDto);
- _context.SaveChanges();
- return Json(_listingProjectsDto);
- }
- return Json(null);
- }
- [HttpPost]
- public JsonResult CreateUser() //objUser is object which should be same as in javascript function
- {
- try
- {
- _context.ListingDTO_DBTable.Add(_listingProjectsDto);
- _context.SaveChanges();
- return Json(_listingProjectsDto); //returning user to javacript
- }
- catch (Exception ex)
- {
- return null;
- }
- }
- public IActionResult Privacy()
- {
- return View();
- }
- [HttpPost]
- public JsonResult DeleteUserJ(int Id)
- {
- try
- {
- var ListingDTO_DBTable = _context.ListingDTO_DBTable.Find(Id); //fetching the user with Id
- if (ListingDTO_DBTable != null)
- {
- _context.ListingDTO_DBTable.Remove(ListingDTO_DBTable); //deleting from db
- _context.SaveChanges();
- return Json(ListingDTO_DBTable);
- }
- return Json(null);
- }
- catch (Exception ex)
- {
- return null;
- }
- }
- [AllowAnonymous]
- public async Task<IActionResult> TestDashboard1()
- {
- var datasource = _context.ListingDTO_DBTable.AsQueryable();
- var query = datasource
- .Select(x => new ListingProjectsDTO() {
- Id = x.Id,
- ListingName = x.ListingName,
- });
- var listings = await query.ToListAsync();
- return View(listings);
- }
- [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
- public IActionResult Error()
- {
- return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment