using Api.SeaHavenIndustries.DTOs; using Data.SeaHavenIndustries; using FluentValidation; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using SendGrid.Helpers.Mail; using SeaHaven.Services.Interfaces; namespace Api.SeaHavenIndustries.Controllers { //[Authorize] // Removed for testing [ApiController] [Route("api/Contact")] [Route("api/contacts")] public class ContactController : Controller { private readonly UserManager _userManager; private readonly IContactService _contactService; private readonly ILocationService _locationService; public ContactController( UserManager userManager, IContactService contactService, ILocationService locationService) { _userManager = userManager; _contactService = contactService; _locationService = locationService; } [HttpPost] [Route("AddContacts")] public async Task AddContacts(Contact_DTO contacts) { try { var userId = _userManager.GetUserId(User); if (userId == null) return Unauthorized(new Response { Status = "Error", Message = "User not authenticated" }); await _contactService.CreateContactAsync(contacts.ToServiceCreateDTO(), userId); 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 }); } catch (Exception ex) { return StatusCode(500, new Response { Status = "Error", Message = ex.Message }); } } [HttpPost] [Route("EditContacts")] public async Task EditContacts(EditContact_DTO contacts) { try { var userId = _userManager.GetUserId(User); if (userId == null) return Unauthorized(new Response { Status = "Error", Message = "User not authenticated" }); await _contactService.UpdateContactAsync(contacts.Id, contacts.ToServiceUpdateDTO(), userId); 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 }); } catch (KeyNotFoundException) { return NotFound(new Response { Status = "Error", Message = "Contact not found" }); } catch (Exception ex) { return StatusCode(500, new Response { Status = "Error", Message = ex.Message }); } } [HttpGet] [Route("DeleteContacts")] public async Task DeleteContacts(int id) { try { var userId = _userManager.GetUserId(User); if (userId == null) return Unauthorized(new Response { Status = "Error", Message = "User not authenticated" }); await _contactService.DeleteContactAsync(id, userId); return Ok(new DataResponse { Message = "Contact deleted successfully", Status = "200" }); } catch (KeyNotFoundException) { return NotFound(new Response { Status = "Error", Message = "Contact not found" }); } catch (Exception ex) { return StatusCode(500, new Response { Status = "Error", Message = ex.Message }); } } [HttpGet] [Route("GetContactList")] public async Task GetContactList(string? search ="", int page = 1, int pageSize = 10) { var pagedResult = await _contactService.GetContactsPagedAsync(page, pageSize, search); var data = ContactListItem_DTO.FromServiceDTOs(pagedResult.Items); var totalPages = (int)Math.Ceiling(pagedResult.TotalCount / (double)pageSize); var viewModel = new Pagination_DTO { Data = data, PageNumber = page, PageSize = pageSize, TotalCount = pagedResult.TotalCount, TotalPages = totalPages }; return Ok(viewModel); } [HttpGet] [Route("GetContacts")] public async Task GetContacts() { var data = await _contactService.GetAllContactsAsync(); return Ok(data); } [HttpGet] [Route("GetLocations")] public async Task GetLocations() { var locations = await _locationService.GetAllLocationsAsync(); var data = LocationViewModel_DTO.FromServiceDTOs(locations); return Ok(data); } [HttpGet] [Route("GetCategories")] public async Task GetCategories() { return StatusCode(501, new Response { Status = "Error", Message = "Not Implemented - Needs CategoryService" }); } [HttpGet] [Route("GetUsers")] public async Task GetUsers() { return StatusCode(501, new Response { Status = "Error", Message = "Not Implemented - Needs UserService" }); } [HttpPost] [Route("AddLocation")] public async Task AddLocation(Location_DTO model) { try { var userId = _userManager.GetUserId(User) ?? "system"; await _locationService.CreateLocationAsync(model.ToServiceCreateDTO(), userId); 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 EditLocation(EditLocation_DTO model) { try { var userId = _userManager.GetUserId(User) ?? "system"; await _locationService.UpdateLocationAsync(model.Id, model.ToServiceUpdateDTO(), userId); return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" }); } catch (KeyNotFoundException) { return BadRequest(new Response { Status = "Error", Message = "ID not matched!" }); } catch (Exception ex) { return BadRequest(new Response { Status = "Error", Message = ex.Message }); } } [HttpPost] [Route("DeleteLocation")] public async Task DeleteLocation(int Id) { try { var userId = _userManager.GetUserId(User) ?? "system"; await _locationService.DeleteLocationAsync(Id, userId); return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" }); } catch (KeyNotFoundException) { return BadRequest(new Response { Status = "Error", Message = "ID not matched!" }); } catch (Exception ex) { return BadRequest(new Response { Status = "Error", Message = ex.Message }); } } [HttpGet] [Route("GetAddressbook")] public async Task GetAddressbook(string? search = "", int page = 1, int pageSize = 10) { var pagedResult = await _locationService.GetLocationsPagedAsync(page, pageSize, search); var data = LocationListItem_DTO.FromServiceDTOs(pagedResult.Items); var totalPages = (int)Math.Ceiling(pagedResult.TotalCount / (double)pageSize); var viewModel = new Pagination_DTO { Data = data, PageNumber = page, PageSize = pageSize, TotalCount = pagedResult.TotalCount, TotalPages = totalPages }; return Ok(viewModel); } } }