Skip to content

Commit

Permalink
Merge pull request #687 from bcgov/yj
Browse files Browse the repository at this point in the history
chore: update platform
  • Loading branch information
ychung-mot authored Sep 27, 2024
2 parents 2b9beed + ba7ccb3 commit 867dc41
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 10 deletions.
18 changes: 17 additions & 1 deletion server/StrDss.Api/Controllers/OrganizationsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public async Task<ActionResult<PlatformViewDto>> GetPlatform(long id)

[ApiAuthorize(Permissions.UserWrite)] //todo: use platform_write permission when it's ready in the database
[HttpPost("platforms", Name = "CreatePlatform")]
public async Task<ActionResult> CreatePlatform(PlatformUpdateDto dto)
public async Task<ActionResult> CreatePlatform(PlatformCreateDto dto)
{
var (errors, id) = await _orgService.CreatePlatformAsync(dto);

Expand All @@ -114,5 +114,21 @@ public async Task<ActionResult> CreatePlatform(PlatformUpdateDto dto)

return Ok(id);
}

[ApiAuthorize(Permissions.UserWrite)] //todo: use platform_write permission when it's ready in the database
[HttpPut("platforms/{id}", Name = "UpdatePlatform")]
public async Task<ActionResult> UpdatePlatform(PlatformUpdateDto dto, long id)
{
dto.OrganizationId = id;

var errors = await _orgService.UpdatePlatformAsync(dto);

if (errors.Any())
{
return ValidationUtils.GetValidationErrorResult(errors, ControllerContext);
}

return Ok();
}
}
}
2 changes: 1 addition & 1 deletion server/StrDss.Data/Mappings/EntityToModelProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public EntityToModelProfile()
.ForMember(o => o.UpdDtm, opt => opt.MapFrom(i => DateUtils.ConvertUtcToPacificTime(i.UpdDtm)));

CreateMap<DssBusinessLicence, BizLicenceSearchDto>();
CreateMap<DssOrganization, PlatformUpdateDto>();
CreateMap<DssOrganization, PlatformCreateDto>();
CreateMap<DssPlatformVw, PlatformViewDto>();
}
}
Expand Down
1 change: 1 addition & 0 deletions server/StrDss.Data/Mappings/ModelToEntityProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public ModelToEntityProfile()
.ForMember(dest => dest.SeparateReservationsQty, opt => opt.MapFrom(src => CommonUtils.StringToShort(src.ReservationsQty)));

CreateMap<RoleUpdateDto, DssUserRole>();
CreateMap<PlatformCreateDto, DssOrganization>();
CreateMap<PlatformUpdateDto, DssOrganization>();
}
}
Expand Down
45 changes: 42 additions & 3 deletions server/StrDss.Data/Repositories/OrganizationRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public interface IOrganizationRepository
Task<StrRequirementsDto?> GetStrRequirements(double longitude, double latitude);
Task<PagedDto<PlatformViewDto>> GetPlatforms(int pageSize, int pageNumber, string orderBy, string direction);
Task<PlatformViewDto?> GetPlatform(long id);
Task<DssOrganization> CreatePlatformAsync(PlatformUpdateDto dto);
Task<DssOrganization> CreatePlatformAsync(PlatformCreateDto dto);
Task<bool> DoesOrgCdExist(string orgCd);
Task UpdatePlatformAsync(PlatformUpdateDto dto);
}
public class OrganizationRepository : RepositoryBase<DssOrganization>, IOrganizationRepository
{
Expand Down Expand Up @@ -178,10 +179,11 @@ public async Task<PagedDto<PlatformViewDto>> GetPlatforms(int pageSize, int page
return platform;
}

public async Task<DssOrganization> CreatePlatformAsync(PlatformUpdateDto dto)
public async Task<DssOrganization> CreatePlatformAsync(PlatformCreateDto dto)
{
var entity = _mapper.Map<DssOrganization>(dto);

entity.OrganizationCd = dto.OrganizationCd.ToUpperInvariant();
entity.OrganizationType = OrganizationTypes.Platform;

await _dbSet.AddAsync(entity);
Expand Down Expand Up @@ -209,7 +211,44 @@ private void CreateContact(DssOrganization entity, string messageType, string? e

public async Task<bool> DoesOrgCdExist(string orgCd)
{
return await _dbSet.AnyAsync(x => x.OrganizationCd == orgCd);
return await _dbSet.AnyAsync(x => x.OrganizationCd == orgCd.ToUpperInvariant());
}

public async Task UpdatePlatformAsync(PlatformUpdateDto dto)
{
var entity = await _dbSet
.Include(x => x.DssOrganizationContactPeople)
.FirstAsync(x => x.OrganizationId == dto.OrganizationId);

_mapper.Map(dto, entity);

UpdateContact(entity, EmailMessageTypes.NoticeOfTakedown, dto.NoticeOfTakedownContactEmail1, true);
UpdateContact(entity, EmailMessageTypes.NoticeOfTakedown, dto.NoticeOfTakedownContactEmail2, false);
UpdateContact(entity, EmailMessageTypes.TakedownRequest, dto.TakedownRequestContactEmail1, true);
UpdateContact(entity, EmailMessageTypes.TakedownRequest, dto.TakedownRequestContactEmail2, false);
}

private void UpdateContact(DssOrganization entity, string messageType, string? emailAddress, bool isPrimary)
{
var contact = entity.DssOrganizationContactPeople
.FirstOrDefault(x => x.EmailMessageType == messageType
&& (isPrimary ? x.IsPrimary == isPrimary : x.IsPrimary == false || x.IsPrimary == null));

if (contact == null)
{
CreateContact(entity, messageType, emailAddress, isPrimary);
}
else
{
if (string.IsNullOrEmpty(emailAddress))
{
_dbContext.DssOrganizationContactPeople.Remove(contact);
}
else
{
contact.EmailAddressDsc = emailAddress;
}
}
}
}
}
13 changes: 13 additions & 0 deletions server/StrDss.Model/OrganizationDtos/PlatformCreateDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace StrDss.Model.OrganizationDtos
{
public class PlatformCreateDto
{
public string OrganizationCd { get; set; } = null!;
public string OrganizationNm { get; set; } = null!;
public DateTime UpdDtm { get; set; }
public string? NoticeOfTakedownContactEmail1 { get; set; }
public string? TakedownRequestContactEmail1 { get; set; }
public string? NoticeOfTakedownContactEmail2 { get; set; }
public string? TakedownRequestContactEmail2 { get; set; }
}
}
1 change: 0 additions & 1 deletion server/StrDss.Model/OrganizationDtos/PlatformUpdateDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
public class PlatformUpdateDto
{
public long OrganizationId { get; set; }
public string OrganizationCd { get; set; } = null!;
public string OrganizationNm { get; set; } = null!;
public DateTime UpdDtm { get; set; }
public string? NoticeOfTakedownContactEmail1 { get; set; }
Expand Down
44 changes: 40 additions & 4 deletions server/StrDss.Service/OrganizationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public interface IOrganizationService
Task<StrRequirementsDto?> GetStrRequirements(double longitude, double latitude);
Task<PagedDto<PlatformViewDto>> GetPlatforms(int pageSize, int pageNumber, string orderBy, string direction);
Task<PlatformViewDto?> GetPlatform(long id);
Task<(Dictionary<string, List<string>>, long)> CreatePlatformAsync(PlatformUpdateDto dto);
Task<(Dictionary<string, List<string>>, long)> CreatePlatformAsync(PlatformCreateDto dto);
Task<Dictionary<string, List<string>>> UpdatePlatformAsync(PlatformUpdateDto dto);
}
public class OrganizationService : ServiceBase, IOrganizationService
{
Expand Down Expand Up @@ -75,11 +76,11 @@ public async Task<PagedDto<PlatformViewDto>> GetPlatforms(int pageSize, int page
return await _orgRepo.GetPlatform(id);
}

public async Task<(Dictionary<string, List<string>>, long)> CreatePlatformAsync(PlatformUpdateDto dto)
public async Task<(Dictionary<string, List<string>>, long)> CreatePlatformAsync(PlatformCreateDto dto)
{
var errors = new Dictionary<string, List<string>>();

await ValidatePlatformUpdateDto(dto, errors);
await ValidatePlatformCreateDto(dto, errors);

if (errors.Any())
{
Expand All @@ -93,7 +94,7 @@ public async Task<PagedDto<PlatformViewDto>> GetPlatforms(int pageSize, int page
return (errors, entity.OrganizationId);
}

private async Task<Dictionary<string, List<string>>> ValidatePlatformUpdateDto(PlatformUpdateDto dto, Dictionary<string, List<string>> errors)
private async Task<Dictionary<string, List<string>>> ValidatePlatformCreateDto(PlatformCreateDto dto, Dictionary<string, List<string>> errors)
{
_validator.Validate(Entities.Platform, dto, errors);

Expand All @@ -109,5 +110,40 @@ private async Task<Dictionary<string, List<string>>> ValidatePlatformUpdateDto(P

return errors;
}

public async Task<Dictionary<string, List<string>>> UpdatePlatformAsync(PlatformUpdateDto dto)
{
var errors = new Dictionary<string, List<string>>();

var platformDto = await _orgRepo.GetPlatform(dto.OrganizationId);

if (platformDto == null)
{
errors.AddItem("OrganizationId", $"Platform with ID {dto.OrganizationId} does not exist");
return errors;
}

await ValidatePlatformUpdateDto(dto, errors);

if (errors.Any())
{
return errors;
}

await _orgRepo.UpdatePlatformAsync(dto);

_unitOfWork.Commit();

return errors;
}

private async Task<Dictionary<string, List<string>>> ValidatePlatformUpdateDto(PlatformUpdateDto dto, Dictionary<string, List<string>> errors)
{
await Task.CompletedTask;

_validator.Validate(Entities.Platform, dto, errors);

return errors;
}
}
}

0 comments on commit 867dc41

Please sign in to comment.