Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #96 from itpartnersillinois/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
bryanjonker-illinois authored Nov 7, 2023
2 parents ea0b7b1 + 604095b commit 1bf0832
Show file tree
Hide file tree
Showing 10 changed files with 795 additions and 97 deletions.
69 changes: 28 additions & 41 deletions ContactAppCore/Api/ContactController.cs
Original file line number Diff line number Diff line change
@@ -1,112 +1,100 @@
using ContactAppCore.Data;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ContactAppCore.Data;
using ContactAppCore.Helpers;
using ContactAppCore.ViewModel;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ContactAppCore.Api
{
namespace ContactAppCore.Api {

[Route("api/[controller]")]
[ApiController]
[AllowAnonymous]
public class ContactController : ControllerBase
{
public class ContactController : ControllerBase {
private IContactRepository contactRepository;

public ContactController(IContactRepository contactRepository)
{
public ContactController(IContactRepository contactRepository) {
this.contactRepository = contactRepository;
}

[HttpGet("Area/{id}")]
public async Task<AreaInformation> GetArea(int id)
{
public async Task<AreaInformation> GetArea(int id) {
var area = await contactRepository.ReadAsync(c => c.Areas.Include(a => a.Offices).Where(o => o.IsActive).SingleOrDefault(o => o.Id == id));
return new AreaInformation(area, true, false, "", null);
return new AreaInformation(area, true, "", null);
}

[HttpGet("AreaCode/{id}")]
public async Task<AreaInformation> GetAreaCode(string id)
{
public async Task<AreaInformation> GetAreaCode(string id) {
var area = await contactRepository.ReadAsync(c => c.Areas.Include(a => a.Offices).Where(a => a.IsActive).SingleOrDefault(a => a.InternalCode == id));
return new AreaInformation(area, true, false, "", null);
return new AreaInformation(area, true, "", null);
}

[HttpGet("AreaCode/External/{id}")]
public async Task<AreaInformation> GetAreaCodeExternal(string id)
{
public async Task<AreaInformation> GetAreaCodeExternal(string id) {
var area = await contactRepository.ReadAsync(c => c.Areas.Include(a => a.Offices).Where(a => a.IsActive && !a.InternalOnly).SingleOrDefault(a => a.InternalCode == id));
return new AreaInformation(area, false, false, "", null);
return new AreaInformation(area, false, "", null);
}

[HttpGet("Area/External/{id}")]
public async Task<AreaInformation> GetAreaExternal(int id)
{
public async Task<AreaInformation> GetAreaExternal(int id) {
var area = await contactRepository.ReadAsync(c => c.Areas.Include(a => a.Offices).Where(o => o.IsActive && !o.InternalOnly).SingleOrDefault(o => o.Id == id));
return new AreaInformation(area, false, false, "", null);
return new AreaInformation(area, false, "", null);
}

[HttpGet("Office/{id}")]
public async Task<OfficeInformation> GetOffice(int id)
{
public async Task<OfficeInformation> GetOffice(int id) {
var office = await contactRepository.ReadAsync(c => c.Offices.Include(a => a.Area).Where(o => o.IsActive).SingleOrDefault(o => o.Id == id));
return new OfficeInformation(office);
}

[HttpGet("OfficeCode/{id}")]
public async Task<OfficeInformation> GetOfficeCode(string id)
{
public async Task<OfficeInformation> GetOfficeCode(string id) {
var office = await contactRepository.ReadAsync(c => c.Offices.Include(a => a.Area).Where(o => o.IsActive).SingleOrDefault(o => o.InternalCode == id));
return new OfficeInformation(office);
}

[HttpGet("Search/Areas")]
public async Task<IEnumerable<AreaInformation>> SearchAreas(string search = "", string areafilter = "", string officefilter = "", bool isCovid = false)
{
public async Task<IEnumerable<AreaInformation>> SearchAreas(string search = "", string areafilter = "", string officefilter = "") {
var areaType = FilterHelper.TranslateArea(areafilter);
var officeType = FilterHelper.TranslateOffice(officefilter);
var areas = await contactRepository.ReadAsync(c => c.Areas.Include(a => a.Offices).Where(a => a.IsActive
&& (search == "" || a.Title.Contains(search) || a.SearchTerms.Contains(search) || a.Audience.Contains(search) ||
a.Offices.Any(o => o.Title.Contains(search)) || a.Offices.Any(o => o.SearchTerms.Contains(search)) || a.Offices.Any(o => o.Audience.Contains(search)))
&& (areaType == null || a.AreaType == areaType) && (a.Offices.Any(o => o.CovidSupport) || !isCovid))
&& (areaType == null || a.AreaType == areaType))
.OrderBy(a => a.InternalOrder).ThenBy(a => a.Title));
return areas.ToList().Select(a => new AreaInformation(a, true, isCovid, search, officeType));
return areas.ToList().Select(a => new AreaInformation(a, true, search, officeType));
}

[HttpGet("Search/Areas/External")]
public async Task<IEnumerable<AreaInformation>> SearchAreasExternal(string search = "", string areafilter = "", string officefilter = "", bool isCovid = false)
{
public async Task<IEnumerable<AreaInformation>> SearchAreasExternal(string search = "", string areafilter = "", string officefilter = "") {
var areaType = FilterHelper.TranslateArea(areafilter);
var officeType = FilterHelper.TranslateOffice(officefilter);
var areas = await contactRepository.ReadAsync(c => c.Areas.Include(a => a.Offices).Where(a => a.IsActive && !a.InternalOnly
&& (search == "" || a.Title.Contains(search) || a.SearchTerms.Contains(search) || a.Audience.Contains(search) ||
a.Offices.Any(o => o.Title.Contains(search)) || a.Offices.Any(o => o.SearchTerms.Contains(search)) || a.Offices.Any(o => o.Audience.Contains(search)))
&& (areaType == null || a.AreaType == areaType) && (a.Offices.Any(o => o.CovidSupport) || !isCovid))
&& (areaType == null || a.AreaType == areaType))
.OrderBy(a => a.InternalOrder).ThenBy(a => a.Title));
return areas.ToList().Select(a => new AreaInformation(a, false, isCovid, search, officeType));
return areas.ToList().Select(a => new AreaInformation(a, false, search, officeType));
}

[HttpGet("Search/Offices")]
public async Task<IEnumerable<OfficeInformation>> SearchOffices(string search = "", string areafilter = "", string officefilter = "", bool isCovid = false)
{
public async Task<IEnumerable<OfficeInformation>> SearchOffices(string search = "", string areafilter = "", string officefilter = "") {
var areaType = FilterHelper.TranslateArea(areafilter);
var officeType = FilterHelper.TranslateOffice(officefilter);
var offices = await contactRepository.ReadAsync(c => c.Offices.Include(a => a.Area).Where(o => o.IsActive
&& (search == "" || o.Title.Contains(search) || o.SearchTerms.Contains(search) || o.Audience.Contains(search) ||
o.Area.Title.Contains(search) || o.Area.SearchTerms.Contains(search) || o.Area.Audience.Contains(search))
&& (areaType == null || o.Area.AreaType == areaType)
&& (officeType == null || o.OfficeType == officeType) && (o.CovidSupport || !isCovid))
&& (officeType == null || o.OfficeType == officeType))
.OrderBy(o => o.Title));
return offices.ToList().Select(o => new OfficeInformation(o));
}

[HttpGet("Search/Offices/ByArea")]
public async Task<IEnumerable<OfficeInformation>> SearchOfficesByArea(int areaId, string search = "", string officefilter = "")
{
public async Task<IEnumerable<OfficeInformation>> SearchOfficesByArea(int areaId, string search = "", string officefilter = "") {
var officeType = FilterHelper.TranslateOffice(officefilter);
var offices = await contactRepository.ReadAsync(c => c.Offices.Include(a => a.Area).Where(o => o.IsActive
&& (search == "" || o.Title.Contains(search) || o.SearchTerms.Contains(search) || o.Audience.Contains(search) ||
Expand All @@ -118,8 +106,7 @@ public async Task<IEnumerable<OfficeInformation>> SearchOfficesByArea(int areaId
}

[HttpGet("Search/Offices/ByAreaCode")]
public async Task<IEnumerable<OfficeInformation>> SearchOfficesByArea(string search, string areaCode, string officefilter)
{
public async Task<IEnumerable<OfficeInformation>> SearchOfficesByArea(string search, string areaCode, string officefilter) {
var officeType = FilterHelper.TranslateOffice(officefilter);
var offices = await contactRepository.ReadAsync(c => c.Offices.Include(a => a.Area).Where(o => o.IsActive
&& (search == "" || o.Title.Contains(search) || o.SearchTerms.Contains(search) || o.Audience.Contains(search) ||
Expand Down
36 changes: 14 additions & 22 deletions ContactAppCore/Api/EditOfficeController.cs
Original file line number Diff line number Diff line change
@@ -1,56 +1,49 @@
using ContactAppCore.Data;
using System.Linq;
using System.Threading.Tasks;
using ContactAppCore.Data;
using ContactAppCore.Data.Models;
using ContactAppCore.Helpers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Linq;
using System.Threading.Tasks;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace ContactAppCore.Api
{
namespace ContactAppCore.Api {

[Route("api/[controller]")]
[ApiController]
public class EditOfficeController : ControllerBase
{
public class EditOfficeController : ControllerBase {
private IContactRepository contactRepository;
private SecurityHelper securityHelper;

public EditOfficeController(IContactRepository contactRepository, SecurityHelper securityHelper)
{
public EditOfficeController(IContactRepository contactRepository, SecurityHelper securityHelper) {
this.contactRepository = contactRepository;
this.securityHelper = securityHelper;
}

[HttpGet("{id}")]
public async Task<Office> Get(int id)
{
if (!securityHelper.AllowOffice(User, id))
{
public async Task<Office> Get(int id) {
if (!securityHelper.AllowOffice(User, id)) {
return default;
}
return await contactRepository.ReadAsync(c => c.Offices.Include(o => o.Area).SingleOrDefault(o => o.Id == id));
}

[HttpPost("Update")]
public async Task<int> Update([FromBody] dynamic json)
{
var jsonObject = (dynamic)JObject.Parse(json.ToString());
public async Task<int> Update([FromBody] dynamic json) {
var jsonObject = (dynamic) JObject.Parse(json.ToString());
int id = int.Parse(jsonObject.id.ToString());
if (!securityHelper.AllowOffice(User, id))
{
if (!securityHelper.AllowOffice(User, id)) {
return default;
}

var originalObject = await contactRepository.ReadAsync(c => c.Offices.SingleOrDefault(o => o.Id == id));
var isAreaAdmin = securityHelper.AllowArea(User, originalObject.AreaId);
await LogHelper.CreateLog(contactRepository, "Editing Area " + originalObject.AreaId.ToString(), User.Identity.Name, JsonConvert.SerializeObject(originalObject), json.ToString());

return await contactRepository.UpdateAsync(new Office
{
return await contactRepository.UpdateAsync(new Office {
Id = id,
AreaId = originalObject.AreaId,
Title = jsonObject.title,
Expand Down Expand Up @@ -89,8 +82,7 @@ public async Task<int> Update([FromBody] dynamic json)
InternalCode = jsonObject.internalcode,
InternalOrder = isAreaAdmin ? jsonObject.internalorder : originalObject.InternalOrder,
IsActive = JsonHelper.TranslateBoolean(jsonObject.isactive),
InternalOnly = isAreaAdmin ? JsonHelper.TranslateBoolean(jsonObject.internalonly) : originalObject.InternalOnly,
CovidSupport = JsonHelper.TranslateBoolean(jsonObject.covidsupport)
InternalOnly = isAreaAdmin ? JsonHelper.TranslateBoolean(jsonObject.internalonly) : originalObject.InternalOnly
});
}
}
Expand Down
2 changes: 0 additions & 2 deletions ContactAppCore/Data/Models/Office.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ public Office(string title, int areaId) {

public string City { get; set; }

public bool CovidSupport { get; set; }

public string Email { get; set; }

public string ExternalUrl { get; set; }
Expand Down
Loading

0 comments on commit 1bf0832

Please sign in to comment.