mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 09:33:13 +00:00
- services/api.js uses lowercase routes (/api/workorders, /api/employees, etc.)
- Add matching [Route] attributes to WorkOrder, Employee, Contact, Account, FollowUp, PMSchedule controllers
- Add [HttpGet("{id:int}")] to WorkOrderController for REST-style get by ID
291 lines
11 KiB
C#
291 lines
11 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] // Removed for testing
|
|
[ApiController]
|
|
[Route("api/Contact")]
|
|
[Route("api/contacts")]
|
|
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.Title = contacts.Title;
|
|
model.Owner = contacts.Owner;
|
|
model.FirstName = contacts.FirstName;
|
|
model.MiddleName = contacts.MiddleName;
|
|
model.LastName = contacts.LastName;
|
|
model.ContactType = contacts.ContactType;
|
|
model.PhoneNumber = contacts.PhoneNumber;
|
|
model.Email = contacts.Email;
|
|
model.Address1 = contacts.Address1;
|
|
model.Address2 = contacts.Address2;
|
|
model.City = contacts.City;
|
|
model.State = contacts.State;
|
|
model.Zip = contacts.Zip;
|
|
model.FacebookUrl = contacts.FacebookUrl;
|
|
model.LinkedInUrl = contacts.LinkedInUrl;
|
|
model.TwitterUrl = contacts.TwitterUrl;
|
|
model.LocationId = contacts.LocationId;
|
|
model.AccountId = contacts.AccountId;
|
|
_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();
|
|
if (exist == null)
|
|
return BadRequest(new Response { Status = "Error", Message = "Contact not found" });
|
|
|
|
exist.Title = contacts.Title;
|
|
exist.Owner = contacts.Owner;
|
|
exist.FirstName = contacts.FirstName;
|
|
exist.MiddleName = contacts.MiddleName;
|
|
exist.LastName = contacts.LastName;
|
|
exist.ContactType = contacts.ContactType;
|
|
exist.PhoneNumber = contacts.PhoneNumber;
|
|
exist.Email = contacts.Email;
|
|
exist.Address1 = contacts.Address1;
|
|
exist.Address2 = contacts.Address2;
|
|
exist.City = contacts.City;
|
|
exist.State = contacts.State;
|
|
exist.Zip = contacts.Zip;
|
|
exist.FacebookUrl = contacts.FacebookUrl;
|
|
exist.LinkedInUrl = contacts.LinkedInUrl;
|
|
exist.TwitterUrl = contacts.TwitterUrl;
|
|
exist.LocationId = contacts.LocationId;
|
|
exist.AccountId = contacts.AccountId;
|
|
_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.FirstName != null && s.FirstName.ToLower().Contains(search.ToLower())) ||
|
|
(s.LastName != null && s.LastName.ToLower().Contains(search.ToLower())) ||
|
|
(s.Email != null && s.Email.ToLower().Contains(search.ToLower())) ||
|
|
(s.PhoneNumber != null && s.PhoneNumber.Contains(search.ToLower()))
|
|
).Select(n => new
|
|
{
|
|
n.Id,
|
|
Name = (n.FirstName + " " + n.LastName).Trim(),
|
|
email = n.Email,
|
|
phone = n.PhoneNumber
|
|
}).OrderByDescending(d => d.Id).Skip((page - 1) * pageSize)
|
|
.Take(pageSize).ToList();
|
|
var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
|
|
|
|
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).Skip((page - 1) * pageSize)
|
|
.Take(pageSize).ToList();
|
|
var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
|
|
|
|
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; }
|
|
}
|
|
|
|
}
|
|
}
|