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

190 lines
6.8 KiB
C#
Raw Normal View History

using Api.SeaHavenIndustries.DTOs;
using Data.SeaHavenIndustries;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace Api.SeaHavenIndustries.Controllers
{
[Authorize]
[ApiController]
[Route("api/[controller]")]
[Route("api/locations")]
public class LocationController : Controller
{
private readonly ApplicationDbContext _db;
public LocationController(ApplicationDbContext db)
{
_db = db;
}
[HttpGet("GetLocationList")]
public IActionResult GetLocationList(string? search = "", int page = 1, int pageSize = 10)
{
var query = _db.Locations.AsQueryable();
if (!string.IsNullOrWhiteSpace(search))
{
var s = search.ToLower();
query = query.Where(l =>
(l.Name ?? "").ToLower().Contains(s) ||
(l.Title ?? "").ToLower().Contains(s) ||
2026-04-28 18:55:14 -05:00
(l.Address1 ?? "").ToLower().Contains(s) ||
(l.City ?? "").ToLower().Contains(s));
}
var totalCount = query.Count();
var data = query
.OrderByDescending(l => l.Id)
.Skip((page - 1) * pageSize)
.Take(pageSize)
.Select(l => new
{
l.Id,
l.Name,
l.Title,
2026-04-28 18:55:14 -05:00
Address = l.Address1, // Database has Address1, not Address
l.Address1,
l.Address2,
l.City,
l.State,
2026-04-28 18:55:14 -05:00
ZipCode = l.Zip, // Database has Zip, not ZipCode
Phone = l.PhoneNumber, // Database has PhoneNumber, not Phone
Contact = (string?)null, // Contact column doesn't exist in database
ContactEmail = l.Email, // Database has Email, not ContactEmail
l.Status
// AccountId doesn't exist in database
})
.ToList();
var viewModel = new Pagination_DTO
{
Data = data,
PageNumber = page,
PageSize = pageSize,
TotalCount = totalCount,
TotalPages = (int)Math.Ceiling(totalCount / (double)pageSize)
};
return Ok(viewModel);
}
[HttpGet("{id}")]
public async Task<IActionResult> GetLocationById(int id)
{
var location = await _db.Locations
.Where(l => l.Id == id)
.Select(l => new
{
l.Id,
l.Name,
l.Title,
2026-04-28 18:55:14 -05:00
Address = l.Address1, // Database has Address1
l.Address1,
l.Address2,
l.City,
l.State,
2026-04-28 18:55:14 -05:00
ZipCode = l.Zip, // Database has Zip
Phone = l.PhoneNumber, // Database has PhoneNumber
Contact = (string?)null, // Contact doesn't exist
ContactEmail = l.Email, // Database has Email
l.Status
// AccountId doesn't exist
})
.FirstOrDefaultAsync();
if (location == null)
return NotFound(new Response { Status = "Error", Message = "Location not found" });
return Ok(location);
}
[HttpPost]
public async Task<IActionResult> AddLocation([FromBody] Location_DTO model)
{
try
{
var location = new Locations
{
Name = model.Name,
Title = model.Title,
2026-04-28 18:55:14 -05:00
Address1 = model.Address, // Map to Address1
City = model.City,
State = model.State,
2026-04-28 18:55:14 -05:00
Zip = model.ZipCode, // Map to Zip
PhoneNumber = model.Phone, // Map to PhoneNumber
Email = model.ContactEmail, // Map to Email
Status = model.Status,
2026-04-28 18:55:14 -05:00
CreatedDate = DateTime.Now
// Contact and AccountId don't exist in database
};
_db.Locations.Add(location);
await _db.SaveChangesAsync();
return Ok(new DataResponse { Message = "Location Created Successfully", Status = "200" });
}
catch (Exception ex)
{
return BadRequest(new Response { Status = "Error", Message = ex.Message });
}
}
[HttpPut("{id}")]
public async Task<IActionResult> EditLocation(int id, [FromBody] EditLocation_DTO model)
{
try
{
var exist = await _db.Locations.Where(l => l.Id == id).FirstOrDefaultAsync();
if (exist == null)
return BadRequest(new Response { Status = "Error", Message = "Location not found" });
exist.Name = model.Name;
exist.Title = model.Title;
2026-04-28 18:55:14 -05:00
exist.Address1 = model.Address; // Map to Address1
exist.City = model.City;
exist.State = model.State;
2026-04-28 18:55:14 -05:00
exist.Zip = model.ZipCode; // Map to Zip
exist.PhoneNumber = model.Phone; // Map to PhoneNumber
exist.Email = model.ContactEmail; // Map to Email
exist.Status = model.Status;
2026-04-28 18:55:14 -05:00
exist.LastModificationTime = DateTime.Now;
// Contact and AccountId don't exist in database
await _db.SaveChangesAsync();
return Ok(new DataResponse { Message = "Location Updated Successfully", Status = "200" });
}
catch (Exception ex)
{
return BadRequest(new Response { Status = "Error", Message = ex.Message });
}
}
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteLocation(int id)
{
try
{
var exist = await _db.Locations.Where(l => l.Id == id).FirstOrDefaultAsync();
if (exist == null)
return BadRequest(new Response { Status = "Error", Message = "Location not found" });
_db.Locations.Remove(exist);
await _db.SaveChangesAsync();
return Ok(new DataResponse { Message = "Location Deleted Successfully", Status = "200" });
}
catch (Exception ex)
{
return BadRequest(new Response { Status = "Error", Message = ex.Message });
}
}
[HttpGet("DeleteLocation")]
public async Task<IActionResult> DeleteLocationByQuery(int id)
{
return await DeleteLocation(id);
}
}
}