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

Commit

Permalink
Merge pull request #170 from AppliedIS/feature-removesave
Browse files Browse the repository at this point in the history
Feature removesave
  • Loading branch information
MrMatt57 committed Nov 28, 2016
2 parents 919d589 + a262bf5 commit 082787f
Show file tree
Hide file tree
Showing 35 changed files with 852 additions and 294 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public static void Register()
container.Register<IApplicationSummaryFactory, ApplicationSummaryFactory>(Lifestyle.Scoped);
container.Register<IStatusRepository, StatusRepository>(Lifestyle.Scoped);
container.Register<IStatusService, StatusService>(Lifestyle.Scoped);
container.Register<IAttachmentRepository, AttachmentRepository>(Lifestyle.Scoped);
container.Register<IAttachmentService, AttachmentService>(Lifestyle.Scoped);

// FluentValidation validators (make this singletons since the overhead of spinning up is high and they have no state)
container.Register<IApplicationSubmissionValidator, ApplicationSubmissionValidator>(Lifestyle.Singleton);
Expand Down
20 changes: 19 additions & 1 deletion DOL.WHD.Section14c.Api/Controllers/ApplicationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

namespace DOL.WHD.Section14c.Api.Controllers
{
/// <summary>
/// Operations on a submitted application
/// </summary>
[AuthorizeHttps]
[RoutePrefix("api/application")]
public class ApplicationController : ApiController
Expand All @@ -22,13 +25,24 @@ public class ApplicationController : ApiController
private readonly IApplicationSubmissionValidator _applicationSubmissionValidator;
private readonly IApplicationSummaryFactory _applicationSummaryFactory;
private readonly IStatusService _statusService;
public ApplicationController(IIdentityService identityService, IApplicationService applicationService, IApplicationSubmissionValidator applicationSubmissionValidator, IApplicationSummaryFactory applicationSummaryFactory, IStatusService statusService)
private readonly ISaveService _saveService;
/// <summary>
/// Default constructor for injecting dependent services
/// </summary>
/// <param name="identityService"></param>
/// <param name="applicationService"></param>
/// <param name="applicationSubmissionValidator"></param>
/// <param name="applicationSummaryFactory"></param>
/// <param name="statusService"></param>
/// <param name="saveService"></param>
public ApplicationController(IIdentityService identityService, IApplicationService applicationService, IApplicationSubmissionValidator applicationSubmissionValidator, IApplicationSummaryFactory applicationSummaryFactory, IStatusService statusService, ISaveService saveService)
{
_identityService = identityService;
_applicationService = applicationService;
_applicationSubmissionValidator = applicationSubmissionValidator;
_applicationSummaryFactory = applicationSummaryFactory;
_statusService = statusService;
_saveService = saveService;
}

/// <summary>
Expand All @@ -55,6 +69,10 @@ public async Task<HttpResponseMessage> Submit([FromBody]ApplicationSubmission su
}

await _applicationService.SubmitApplicationAsync(submission);

// remove the associated application save
_saveService.Remove(submission.EIN);

return Request.CreateResponse(HttpStatusCode.Created);
}

Expand Down
12 changes: 6 additions & 6 deletions DOL.WHD.Section14c.Api/Controllers/AttachmentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ namespace DOL.WHD.Section14c.Api.Controllers
[RoutePrefix("api/attachment")]
public class AttachmentController : ApiController
{
private readonly ISaveService _saveService;
private readonly IAttachmentService _attachmentService;
private readonly IIdentityService _identityService;

public AttachmentController(ISaveService saveService, IIdentityService identityService)
public AttachmentController(IAttachmentService attachmentService, IIdentityService identityService)
{
_saveService = saveService;
_attachmentService = attachmentService;
_identityService = identityService;
}

Expand Down Expand Up @@ -60,7 +60,7 @@ public async Task<IHttpActionResult> Post(string EIN)
await stream.CopyToAsync(memoryStream);
var fileName = stream.Headers.ContentDisposition.FileName.Replace("\"", "");
var fileType = stream.Headers.ContentType.MediaType.Replace("\"", "");
var fileUpload = _saveService.UploadAttachment(EIN, memoryStream, fileName, fileType);
var fileUpload = _attachmentService.UploadAttachment(EIN, memoryStream, fileName, fileType);
files.Add(fileUpload);
}

Expand Down Expand Up @@ -90,7 +90,7 @@ public HttpResponseMessage Download(string EIN, Guid fileId)
{
var memoryStream = new MemoryStream(); // Disponsed by Framework

var attachmentDownload = _saveService.DownloadAttachment(memoryStream, EIN, fileId);
var attachmentDownload = _attachmentService.DownloadAttachment(memoryStream, EIN, fileId);

var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Expand Down Expand Up @@ -135,7 +135,7 @@ public IHttpActionResult Delete(string EIN, Guid fileId)

try
{
_saveService.DeleteAttachement(EIN, fileId);
_attachmentService.DeleteAttachement(EIN, fileId);
}
catch (ObjectNotFoundException)
{
Expand Down
29 changes: 29 additions & 0 deletions DOL.WHD.Section14c.Api/Controllers/SaveController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@

namespace DOL.WHD.Section14c.Api.Controllers
{
/// <summary>
/// Controller to manage application save states
/// </summary>
[AuthorizeHttps]
[RoutePrefix("api/save")]
public class SaveController : ApiController
{
private readonly ISaveService _saveService;
private readonly IIdentityService _identityService;

/// <summary>
/// Constructor to inject services
/// </summary>
/// <param name="saveService"></param>
/// <param name="identityService"></param>
public SaveController(ISaveService saveService, IIdentityService identityService)
{
_saveService = saveService;
Expand Down Expand Up @@ -78,6 +86,27 @@ public IHttpActionResult AddSave(string EIN)
_saveService.AddOrUpdate(EIN, state);
return Created($"/api/Save?userId={User.Identity.GetUserId()}&EIN={EIN}", new { });
}

/// <summary>
/// Removes pre-submission 14c application
/// </summary>
/// <param name="EIN"></param>
/// <returns></returns>
[HttpDelete]
[Route("{EIN}")]
[AuthorizeClaims(ApplicationClaimTypes.SubmitApplication)]
public IHttpActionResult DeleteSave(string EIN)
{
// make sure user has rights to the EIN
var hasEINClaim = _identityService.UserHasEINClaim(User, EIN);
if (!hasEINClaim)
{
return Unauthorized();
}

_saveService.Remove(EIN);
return Ok();
}

/// <summary>
/// OPTIONS endpoint for CORS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="IReCaptchaService.cs" />
<Compile Include="Services\ApplicationService.cs" />
<Compile Include="Services\AttachmentService.cs" />
<Compile Include="IAttachmentService.cs" />
<Compile Include="Services\IdentityService.cs" />
<Compile Include="Services\ReCaptchaService.cs" />
<Compile Include="Services\ResponseService.cs" />
Expand Down
14 changes: 14 additions & 0 deletions DOL.WHD.Section14c.Business/IAttachmentService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.IO;
using DOL.WHD.Section14c.Domain.Models.Submission;
using DOL.WHD.Section14c.Domain.ViewModels;

namespace DOL.WHD.Section14c.Business
{
public interface IAttachmentService : IDisposable
{
Attachment UploadAttachment(string EIN, MemoryStream memoryStream, string fileName, string fileType);
AttachementDownload DownloadAttachment(MemoryStream memoryStream, string EIN, Guid fileId);
void DeleteAttachement(string EIN, Guid fileId);
}
}
13 changes: 1 addition & 12 deletions DOL.WHD.Section14c.Business/ISaveService.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using System;
using System.IO;
using DOL.WHD.Section14c.Domain.Models;
using DOL.WHD.Section14c.Domain.Models.Identity;
using DOL.WHD.Section14c.Domain.Models.Submission;
using DOL.WHD.Section14c.Domain.ViewModels;


namespace DOL.WHD.Section14c.Business
{
Expand All @@ -13,12 +8,6 @@ public interface ISaveService : IDisposable
ApplicationSave GetSave(string EIN);

void AddOrUpdate(string EIN, string state);

Attachment UploadAttachment(string EIN, MemoryStream memoryStream, string fileName, string fileType);

AttachementDownload DownloadAttachment(MemoryStream memoryStream, string EIN, Guid fileId);

void DeleteAttachement(string EIN, Guid fileId);

void Remove(string EIN);
}
}
4 changes: 2 additions & 2 deletions DOL.WHD.Section14c.Business/Services/ApplicationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ private void CleanupWageTypeInfo(WageTypeInfo wageTypeInfo)
if (prevailingWageMethod == ResponseIds.PrevailingWageMethod.PrevailingWageSurvey)
{
wageTypeInfo.AlternateWageData = null;
wageTypeInfo.SCAWageDeterminationId = null;
wageTypeInfo.SCAWageDeterminationAttachmentId = null;
}
else if (prevailingWageMethod == ResponseIds.PrevailingWageMethod.AlternateWageData)
{
wageTypeInfo.MostRecentPrevailingWageSurvey = null;
wageTypeInfo.SCAWageDeterminationId = null;
wageTypeInfo.SCAWageDeterminationAttachmentId = null;
}
else if (prevailingWageMethod == ResponseIds.PrevailingWageMethod.SCAWageDetermination)
{
Expand Down
80 changes: 80 additions & 0 deletions DOL.WHD.Section14c.Business/Services/AttachmentService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Data;
using System.IO;
using System.Linq;
using DOL.WHD.Section14c.DataAccess;
using DOL.WHD.Section14c.Domain.Models.Submission;
using DOL.WHD.Section14c.Domain.ViewModels;

namespace DOL.WHD.Section14c.Business.Services
{
public class AttachmentService : IAttachmentService
{
private readonly IFileRepository _fileRepository;
private readonly IAttachmentRepository _attachmentRepository;

public AttachmentService(IFileRepository fileRepository, IAttachmentRepository attachmentRepository)
{
_fileRepository = fileRepository;
_attachmentRepository = attachmentRepository;
}

public Attachment UploadAttachment(string EIN, MemoryStream memoryStream, string fileName, string fileType)
{
var fileUpload = new Attachment()
{
FileSize = memoryStream.Length,
MimeType = fileType,
OriginalFileName = fileName,
Deleted = false,
EIN = EIN
};

fileUpload.RepositoryFilePath = $@"{EIN}\{fileUpload.Id}";

_fileRepository.Upload(memoryStream, fileUpload.RepositoryFilePath);

_attachmentRepository.Add(fileUpload);
_attachmentRepository.SaveChanges();

return fileUpload;
}

public AttachementDownload DownloadAttachment(MemoryStream memoryStream, string EIN, Guid fileId)
{
var attachment = _attachmentRepository.Get()
.Where(x => x.EIN == EIN)
.SingleOrDefault(x => x.Deleted == false && x.Id == fileId);

if (attachment == null)
throw new ObjectNotFoundException();

var stream = _fileRepository.Download(memoryStream, attachment.RepositoryFilePath);

return new AttachementDownload()
{
MemoryStream = stream,
Attachment = attachment
};
}

public void DeleteAttachement(string EIN, Guid fileId)
{
var attachment = _attachmentRepository.Get()
.Where(x => x.EIN == EIN)
.SingleOrDefault(x => x.Deleted == false && x.Id == fileId);

if (attachment == null)
throw new ObjectNotFoundException();

attachment.Deleted = true;

_attachmentRepository.SaveChanges();
}

public void Dispose()
{
_attachmentRepository.Dispose();
}
}
}
72 changes: 4 additions & 68 deletions DOL.WHD.Section14c.Business/Services/SaveService.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Linq;
using DOL.WHD.Section14c.DataAccess;
using DOL.WHD.Section14c.Domain.Models;
using DOL.WHD.Section14c.Domain.Models.Identity;
using DOL.WHD.Section14c.Domain.Models.Submission;
using DOL.WHD.Section14c.Domain.ViewModels;

namespace DOL.WHD.Section14c.Business.Services
{
public class SaveService : ISaveService
{
private readonly ISaveRepository _repository;
private readonly IFileRepository _fileRepository;

public SaveService(ISaveRepository repository, IFileRepository fileRepository)
public SaveService(ISaveRepository repository)
{
_repository = repository;
_fileRepository = fileRepository;
}

public ApplicationSave GetSave(string EIN)
Expand Down Expand Up @@ -49,64 +40,9 @@ public void AddOrUpdate(string EIN, string state)
}
}

public Attachment UploadAttachment(string EIN, MemoryStream memoryStream, string fileName, string fileType)
public void Remove(string EIN)
{
var fileUpload = new Domain.Models.Submission.Attachment()
{
FileSize = memoryStream.Length,
MimeType = fileType,
OriginalFileName = fileName,
Deleted = false
};

fileUpload.RepositoryFilePath = $@"{EIN}\{fileUpload.Id}";

_fileRepository.Upload(memoryStream, fileUpload.RepositoryFilePath);

var applicationSave = _repository.Get().SingleOrDefault(x => x.EIN == EIN);
if (applicationSave == null)
{
applicationSave = new ApplicationSave() { EIN = EIN, ApplicationState = "{}" };
_repository.Add(applicationSave);
}

applicationSave.Attachments.Add(fileUpload);
_repository.SaveChanges();

return fileUpload;
}

public AttachementDownload DownloadAttachment(MemoryStream memoryStream, string EIN, Guid fileId)
{
var attachment = _repository.Get()
.Where(x => x.EIN == EIN)
.SelectMany(x => x.Attachments)
.SingleOrDefault(x => x.Deleted == false && x.Id == fileId);

if (attachment == null)
throw new ObjectNotFoundException();

var stream = _fileRepository.Download(memoryStream, attachment.RepositoryFilePath);

return new AttachementDownload()
{
MemoryStream = stream,
Attachment = attachment
};
}

public void DeleteAttachement(string EIN, Guid fileId)
{
var attachment = _repository.Get()
.Where(x => x.EIN == EIN)
.SelectMany(x => x.Attachments)
.SingleOrDefault(x => x.Deleted == false && x.Id == fileId);

if (attachment == null)
throw new ObjectNotFoundException();

attachment.Deleted = true;

_repository.Remove(EIN);
_repository.SaveChanges();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public WageTypeInfoValidator(IPrevailingWageSurveyInfoValidator prevailingWageSu
.NotNull()
.SetValidator(alternateWageDataValidator)
.When(w => w.PrevailingWageMethodId == ResponseIds.PrevailingWageMethod.AlternateWageData);
RuleFor(w => w.SCAWageDeterminationId)
RuleFor(w => w.SCAWageDeterminationAttachmentId)
.NotNull()
.When(w => w.PrevailingWageMethodId == ResponseIds.PrevailingWageMethod.SCAWageDetermination);
}
Expand Down
Loading

0 comments on commit 082787f

Please sign in to comment.