mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 04:53:11 +00:00
257 lines
9.3 KiB
C#
257 lines
9.3 KiB
C#
using Api.SeaHavenIndustries.DTOs;
|
|
using Data.SeaHavenIndustries;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using SendGrid.Helpers.Mail;
|
|
|
|
namespace Api.SeaHavenIndustries.Controllers
|
|
{
|
|
[Authorize]
|
|
[ApiController]
|
|
[Route("api/Contact")]
|
|
public class ContactController : Controller
|
|
{
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
|
private readonly ApplicationDbContext _db;
|
|
public ContactController(UserManager<ApplicationUser> userManager, ApplicationDbContext db)
|
|
{
|
|
_userManager = userManager;
|
|
_db = db;
|
|
}
|
|
[HttpPost]
|
|
[Route("AddContacts")]
|
|
public async Task<IActionResult> AddContacts(Contact_DTO contacts)
|
|
{
|
|
try
|
|
{
|
|
Contacts model = new Contacts();
|
|
model.Name = contacts.Name;
|
|
model.PhoneNumber = contacts.PhoneNumber;
|
|
model.Email = contacts.Email;
|
|
_db.Contacts.Add(model);
|
|
await _db.SaveChangesAsync();
|
|
return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new Response { Status = "Error", Message = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("EditContacts")]
|
|
public async Task<IActionResult> EditContacts(EditContact_DTO contacts)
|
|
{
|
|
try
|
|
{
|
|
var exist = _db.Contacts.Where(w => w.Id == contacts.Id).FirstOrDefault();
|
|
|
|
exist.Name = contacts.Name;
|
|
exist.PhoneNumber = contacts.PhoneNumber;
|
|
exist.Email = contacts.Email;
|
|
_db.Contacts.Update(exist);
|
|
await _db.SaveChangesAsync();
|
|
return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new Response { Status = "Error", Message = ex.Message });
|
|
}
|
|
}
|
|
[HttpGet]
|
|
[Route("DeleteContacts")]
|
|
public async Task<IActionResult> DeleteContacts(int id)
|
|
{
|
|
try
|
|
{
|
|
var exist = _db.Contacts.Where(w => w.Id == id).FirstOrDefault();
|
|
if (exist != null)
|
|
{
|
|
_db.Remove(exist);
|
|
}
|
|
else
|
|
{
|
|
return BadRequest(new Response { Status = "Error", Message = "ID not matched!" });
|
|
}
|
|
await _db.SaveChangesAsync();
|
|
return Ok(new DataResponse { Message = "Removed Successfully", Status = "200" });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new Response { Status = "Error", Message = ex.Message });
|
|
}
|
|
}
|
|
[HttpGet]
|
|
[Route("GetContactList")]
|
|
public IActionResult GetContactList(string? search ="", int page = 1, int pageSize = 10)
|
|
{
|
|
var totalCount = _db.Contacts.Count();
|
|
var data = _db.Contacts.Where(s=> s.Name.ToLower().Contains(search.ToLower()) || s.Email.ToLower().Contains(search.ToLower()) || s.PhoneNumber.Contains(search.ToLower())).Select(n => new
|
|
{
|
|
n.Id,
|
|
n.Name,
|
|
email = n.Email,
|
|
phone = n.PhoneNumber
|
|
}).OrderByDescending(d => d.Id).ToList().Skip((page - 1) * pageSize)
|
|
.Take(pageSize);
|
|
// Calculate the number of pages
|
|
var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
|
|
|
|
// Create a view model containing the paged items and pagination metadata
|
|
var viewModel = new Pagination_DTO
|
|
{
|
|
Data = data,
|
|
PageNumber = page,
|
|
PageSize = pageSize,
|
|
TotalCount = totalCount,
|
|
TotalPages = totalPages
|
|
};
|
|
return Ok(viewModel);
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("GetContacts")]
|
|
public async Task<IActionResult> GetContacts()
|
|
{
|
|
var data = await _db.Contacts.ToListAsync();
|
|
return Ok(data);
|
|
}
|
|
[HttpGet]
|
|
[Route("GetLocations")]
|
|
public async Task<IActionResult> GetLocations()
|
|
{
|
|
var data = await _db.Locations.Select(s => new LocationViewModel
|
|
{
|
|
Id = s.Id,
|
|
Name = s.Title + " - " + s.Name
|
|
}).ToListAsync();
|
|
return Ok(data);
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("GetCategories")]
|
|
public async Task<IActionResult> GetCategories()
|
|
{
|
|
var data = await _db.Categories.ToListAsync();
|
|
return Ok(data);
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("GetUsers")]
|
|
public async Task<IActionResult> GetUsers()
|
|
{
|
|
var filteredUsers = await (from user in _db.Users
|
|
where !(from userRole in _db.UserRoles
|
|
join role in _db.Roles on userRole.RoleId equals role.Id
|
|
where role.Name == "Admin" || role.Name == "Manager"
|
|
select userRole.UserId).Contains(user.Id)
|
|
select user).ToListAsync();
|
|
return Ok(filteredUsers);
|
|
}
|
|
[HttpPost]
|
|
[Route("AddLocation")]
|
|
public async Task<IActionResult> AddLocation(Location_DTO model)
|
|
{
|
|
try
|
|
{
|
|
Locations locations = new Locations();
|
|
locations.Title = model.Title;
|
|
locations.Name = model.Name;
|
|
_db.Locations.Add(locations);
|
|
await _db.SaveChangesAsync();
|
|
return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new Response { Status = "Error", Message = ex.Message });
|
|
}
|
|
}
|
|
[HttpPost]
|
|
[Route("EditLocation")]
|
|
public async Task<IActionResult> EditLocation(EditLocation_DTO model)
|
|
{
|
|
try
|
|
{
|
|
var exist = _db.Locations.Where(w => w.Id == model.Id).FirstOrDefault();
|
|
if (exist != null)
|
|
{
|
|
exist.Title = model.Title;
|
|
exist.Name = model.Name;
|
|
}
|
|
else
|
|
{
|
|
return BadRequest(new Response { Status = "Error", Message = "ID not matched!" });
|
|
}
|
|
_db.Locations.Update(exist);
|
|
await _db.SaveChangesAsync();
|
|
return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new Response { Status = "Error", Message = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("DeleteLocation")]
|
|
public async Task<IActionResult> DeleteLocation(int Id)
|
|
{
|
|
try
|
|
{
|
|
var data1 = await _db.Templates.ToListAsync();
|
|
_db.Templates.RemoveRange(data1);
|
|
var data = await _db.Locations.Where(w => w.Id == Id).FirstOrDefaultAsync();
|
|
if (data != null)
|
|
{
|
|
_db.Locations.Remove(data ?? new Locations());
|
|
}
|
|
else
|
|
{
|
|
return BadRequest(new Response { Status = "Error", Message = "ID not matched!" });
|
|
}
|
|
await _db.SaveChangesAsync();
|
|
return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new Response { Status = "Error", Message = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("GetAddressbook")]
|
|
public IActionResult GetAddressbook(string? search = "", int page = 1, int pageSize = 10)
|
|
{
|
|
var totalCount = _db.Locations.Count();
|
|
var data = _db.Locations.Where(w=> w.Name.ToLower().Contains(search.ToLower()) || w.Title.ToLower().Contains(search.ToLower())).Select(n => new
|
|
{
|
|
n.Id,
|
|
n.Name,
|
|
n.Title
|
|
}).OrderByDescending(d => d.Id).ToList().Skip((page - 1) * pageSize)
|
|
.Take(pageSize);
|
|
// Calculate the number of pages
|
|
var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
|
|
|
|
// Create a view model containing the paged items and pagination metadata
|
|
var viewModel = new Pagination_DTO
|
|
{
|
|
Data = data,
|
|
PageNumber = page,
|
|
PageSize = pageSize,
|
|
TotalCount = totalCount,
|
|
TotalPages = totalPages
|
|
};
|
|
return Ok(viewModel);
|
|
}
|
|
|
|
public class LocationViewModel
|
|
{
|
|
public int? Id { get; set; }
|
|
public string? Name { get; set; }
|
|
}
|
|
|
|
}
|
|
}
|