shoc-backend/Api.SeaHavenIndustries/Controllers/ContactController.cs

235 lines
8.6 KiB
C#
Raw Normal View History

2024-09-28 14:58:36 -04:00
using Api.SeaHavenIndustries.DTOs;
using Data.SeaHavenIndustries;
2026-05-14 11:00:12 -05:00
using FluentValidation;
2024-09-28 14:58:36 -04:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
2025-02-18 13:13:33 -06:00
using SendGrid.Helpers.Mail;
2026-05-06 10:49:33 -05:00
using SeaHaven.Services.Interfaces;
2024-09-28 14:58:36 -04:00
namespace Api.SeaHavenIndustries.Controllers
{
2026-01-21 16:24:54 -06:00
//[Authorize] // Removed for testing
2024-09-28 14:58:36 -04:00
[ApiController]
2025-02-18 13:13:33 -06:00
[Route("api/Contact")]
[Route("api/contacts")]
2024-09-28 14:58:36 -04:00
public class ContactController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
2026-05-06 10:49:33 -05:00
private readonly IContactService _contactService;
private readonly ILocationService _locationService;
public ContactController(
UserManager<ApplicationUser> userManager,
IContactService contactService,
ILocationService locationService)
2024-09-28 14:58:36 -04:00
{
_userManager = userManager;
2026-05-06 10:49:33 -05:00
_contactService = contactService;
_locationService = locationService;
2024-09-28 14:58:36 -04:00
}
[HttpPost]
[Route("AddContacts")]
public async Task<IActionResult> AddContacts(Contact_DTO contacts)
{
try
{
2026-05-14 11:00:12 -05:00
var userId = _userManager.GetUserId(User);
if (userId == null)
return Unauthorized(new Response { Status = "Error", Message = "User not authenticated" });
2026-05-06 10:49:33 -05:00
await _contactService.CreateContactAsync(contacts.ToServiceCreateDTO(), userId);
2026-05-14 11:00:12 -05:00
return Ok(new DataResponse { Message = "Contact created successfully", Status = "200" });
}
catch (ValidationException vex)
{
var errors = string.Join(", ", vex.Errors.Select(e => e.ErrorMessage));
return BadRequest(new Response { Status = "Validation Error", Message = errors });
2024-09-28 14:58:36 -04:00
}
catch (Exception ex)
{
2026-05-14 11:00:12 -05:00
return StatusCode(500, new Response { Status = "Error", Message = ex.Message });
2024-09-28 14:58:36 -04:00
}
}
[HttpPost]
[Route("EditContacts")]
public async Task<IActionResult> EditContacts(EditContact_DTO contacts)
{
try
{
2026-05-14 11:00:12 -05:00
var userId = _userManager.GetUserId(User);
if (userId == null)
return Unauthorized(new Response { Status = "Error", Message = "User not authenticated" });
2026-05-06 10:49:33 -05:00
await _contactService.UpdateContactAsync(contacts.Id, contacts.ToServiceUpdateDTO(), userId);
2024-09-28 14:58:36 -04:00
2026-05-14 11:00:12 -05:00
return Ok(new DataResponse { Message = "Contact updated successfully", Status = "200" });
}
catch (ValidationException vex)
{
var errors = string.Join(", ", vex.Errors.Select(e => e.ErrorMessage));
return BadRequest(new Response { Status = "Validation Error", Message = errors });
2024-09-28 14:58:36 -04:00
}
2026-05-06 10:49:33 -05:00
catch (KeyNotFoundException)
{
2026-05-14 11:00:12 -05:00
return NotFound(new Response { Status = "Error", Message = "Contact not found" });
2026-05-06 10:49:33 -05:00
}
2024-09-28 14:58:36 -04:00
catch (Exception ex)
{
2026-05-14 11:00:12 -05:00
return StatusCode(500, new Response { Status = "Error", Message = ex.Message });
2024-09-28 14:58:36 -04:00
}
}
2026-05-06 10:49:33 -05:00
2024-09-28 14:58:36 -04:00
[HttpGet]
[Route("DeleteContacts")]
public async Task<IActionResult> DeleteContacts(int id)
{
try
{
2026-05-14 11:00:12 -05:00
var userId = _userManager.GetUserId(User);
if (userId == null)
return Unauthorized(new Response { Status = "Error", Message = "User not authenticated" });
2026-05-06 10:49:33 -05:00
await _contactService.DeleteContactAsync(id, userId);
2026-05-14 11:00:12 -05:00
return Ok(new DataResponse { Message = "Contact deleted successfully", Status = "200" });
2024-09-28 14:58:36 -04:00
}
2026-05-06 10:49:33 -05:00
catch (KeyNotFoundException)
{
2026-05-14 11:00:12 -05:00
return NotFound(new Response { Status = "Error", Message = "Contact not found" });
2026-05-06 10:49:33 -05:00
}
2024-09-28 14:58:36 -04:00
catch (Exception ex)
{
2026-05-14 11:00:12 -05:00
return StatusCode(500, new Response { Status = "Error", Message = ex.Message });
2024-09-28 14:58:36 -04:00
}
}
[HttpGet]
[Route("GetContactList")]
2026-05-06 10:49:33 -05:00
public async Task<IActionResult> GetContactList(string? search ="", int page = 1, int pageSize = 10)
2024-09-28 14:58:36 -04:00
{
2026-05-06 10:49:33 -05:00
var pagedResult = await _contactService.GetContactsPagedAsync(page, pageSize, search);
var data = ContactListItem_DTO.FromServiceDTOs(pagedResult.Items);
var totalPages = (int)Math.Ceiling(pagedResult.TotalCount / (double)pageSize);
2024-09-28 14:58:36 -04:00
var viewModel = new Pagination_DTO
{
Data = data,
PageNumber = page,
PageSize = pageSize,
2026-05-06 10:49:33 -05:00
TotalCount = pagedResult.TotalCount,
2024-09-28 14:58:36 -04:00
TotalPages = totalPages
};
return Ok(viewModel);
}
2025-02-18 13:13:33 -06:00
[HttpGet]
[Route("GetContacts")]
public async Task<IActionResult> GetContacts()
{
2026-05-06 10:49:33 -05:00
var data = await _contactService.GetAllContactsAsync();
2025-02-18 13:13:33 -06:00
return Ok(data);
}
[HttpGet]
[Route("GetLocations")]
public async Task<IActionResult> GetLocations()
{
2026-05-06 10:49:33 -05:00
var locations = await _locationService.GetAllLocationsAsync();
var data = LocationViewModel_DTO.FromServiceDTOs(locations);
2025-02-18 13:13:33 -06:00
return Ok(data);
}
[HttpGet]
[Route("GetCategories")]
public async Task<IActionResult> GetCategories()
{
2026-05-06 10:49:33 -05:00
return StatusCode(501, new Response { Status = "Error", Message = "Not Implemented - Needs CategoryService" });
2025-02-18 13:13:33 -06:00
}
[HttpGet]
[Route("GetUsers")]
public async Task<IActionResult> GetUsers()
{
2026-05-06 10:49:33 -05:00
return StatusCode(501, new Response { Status = "Error", Message = "Not Implemented - Needs UserService" });
2025-02-18 13:13:33 -06:00
}
2026-05-06 10:49:33 -05:00
2024-09-28 14:58:36 -04:00
[HttpPost]
[Route("AddLocation")]
public async Task<IActionResult> AddLocation(Location_DTO model)
{
try
{
2026-05-06 10:49:33 -05:00
var userId = _userManager.GetUserId(User) ?? "system";
await _locationService.CreateLocationAsync(model.ToServiceCreateDTO(), userId);
2024-09-28 14:58:36 -04:00
return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" });
}
catch (Exception ex)
{
return BadRequest(new Response { Status = "Error", Message = ex.Message });
}
}
2026-05-06 10:49:33 -05:00
2024-09-28 14:58:36 -04:00
[HttpPost]
[Route("EditLocation")]
public async Task<IActionResult> EditLocation(EditLocation_DTO model)
{
try
{
2026-05-06 10:49:33 -05:00
var userId = _userManager.GetUserId(User) ?? "system";
await _locationService.UpdateLocationAsync(model.Id, model.ToServiceUpdateDTO(), userId);
2024-09-28 14:58:36 -04:00
return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" });
}
2026-05-06 10:49:33 -05:00
catch (KeyNotFoundException)
{
return BadRequest(new Response { Status = "Error", Message = "ID not matched!" });
}
2024-09-28 14:58:36 -04:00
catch (Exception ex)
{
return BadRequest(new Response { Status = "Error", Message = ex.Message });
}
}
[HttpPost]
[Route("DeleteLocation")]
public async Task<IActionResult> DeleteLocation(int Id)
{
try
{
2026-05-06 10:49:33 -05:00
var userId = _userManager.GetUserId(User) ?? "system";
await _locationService.DeleteLocationAsync(Id, userId);
2024-09-28 14:58:36 -04:00
return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" });
}
2026-05-06 10:49:33 -05:00
catch (KeyNotFoundException)
{
return BadRequest(new Response { Status = "Error", Message = "ID not matched!" });
}
2024-09-28 14:58:36 -04:00
catch (Exception ex)
{
return BadRequest(new Response { Status = "Error", Message = ex.Message });
}
}
[HttpGet]
[Route("GetAddressbook")]
2026-05-06 10:49:33 -05:00
public async Task<IActionResult> GetAddressbook(string? search = "", int page = 1, int pageSize = 10)
2024-09-28 14:58:36 -04:00
{
2026-05-06 10:49:33 -05:00
var pagedResult = await _locationService.GetLocationsPagedAsync(page, pageSize, search);
var data = LocationListItem_DTO.FromServiceDTOs(pagedResult.Items);
var totalPages = (int)Math.Ceiling(pagedResult.TotalCount / (double)pageSize);
2024-09-28 14:58:36 -04:00
var viewModel = new Pagination_DTO
{
Data = data,
PageNumber = page,
PageSize = pageSize,
2026-05-06 10:49:33 -05:00
TotalCount = pagedResult.TotalCount,
2024-09-28 14:58:36 -04:00
TotalPages = totalPages
};
return Ok(viewModel);
}
}
}