Skip to content

Commit ecdbd3b

Browse files
authored
Merge pull request #4 from fossapps/get-applications
feat(applications): add get applications endpoint
2 parents 5aef50f + 1daaf64 commit ecdbd3b

File tree

8 files changed

+177
-5
lines changed

8 files changed

+177
-5
lines changed

Micro.AppRegistration.Api/CreateApplication/AppRegistrationController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public AppRegistrationController(IAppRegistrationService appRegistrationService)
2020

2121
[HttpPost]
2222
[Authorize]
23-
public async Task<IActionResult> Test(CreateApplicationRequest request)
23+
public async Task<IActionResult> CreateApplication(CreateApplicationRequest request)
2424
{
2525
try
2626
{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
3+
namespace Micro.AppRegistration.Api.ListApplications
4+
{
5+
public class ApplicationResponse
6+
{
7+
public string Id { set; get; }
8+
public string Name { set; get; }
9+
public string User { set; get; }
10+
public string ShortCode { set; get; }
11+
public bool UseDefaultCode { set; get; }
12+
public bool Approved { set; get; }
13+
public DateTime CreatedAt { set; get; }
14+
public string CreatedBy { set; get; }
15+
}
16+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Micro.AppRegistration.Api.UserDataExtensions;
4+
using Microsoft.AspNetCore.Authorization;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.AspNetCore.Mvc;
7+
8+
namespace Micro.AppRegistration.Api.ListApplications
9+
{
10+
[ApiController]
11+
[Route("api/[controller]")]
12+
public class ApplicationsController : ControllerBase
13+
{
14+
private readonly IListApplicationService _listApplication;
15+
16+
public ApplicationsController(IListApplicationService listApplication)
17+
{
18+
_listApplication = listApplication;
19+
}
20+
21+
[Authorize]
22+
[HttpGet]
23+
public async Task<IActionResult> ListApplications()
24+
{
25+
try
26+
{
27+
return Ok(await _listApplication.FindByOwner(this.GetUserId()));
28+
}
29+
catch (Exception e)
30+
{
31+
return StatusCode(StatusCodes.Status500InternalServerError, new ProblemDetails
32+
{
33+
Title = "internal server error"
34+
});
35+
}
36+
}
37+
38+
[Authorize]
39+
[HttpGet("{id}")]
40+
public async Task<IActionResult> GetApplication(string id)
41+
{
42+
try
43+
{
44+
return Ok(await _listApplication.FindByAppId(id, this.GetUserId()));
45+
}
46+
catch (UnauthorizedRequestException e)
47+
{
48+
return StatusCode(StatusCodes.Status401Unauthorized, new ProblemDetails
49+
{
50+
Title = "you don't have access to this application"
51+
});
52+
}
53+
catch (Exception e)
54+
{
55+
return StatusCode(StatusCodes.Status500InternalServerError, new ProblemDetails
56+
{
57+
Title = "internal server error"
58+
});
59+
}
60+
}
61+
}
62+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using Micro.AppRegistration.Api.Models;
5+
using Microsoft.EntityFrameworkCore;
6+
7+
namespace Micro.AppRegistration.Api.ListApplications
8+
{
9+
public interface IListApplicationRepository
10+
{
11+
public Task<IEnumerable<Application>> ListApplicationsByOwner(string ownerId);
12+
public Task<Application> FindById(string id);
13+
}
14+
public class ListApplicationRepository : IListApplicationRepository
15+
{
16+
private readonly ApplicationContext _db;
17+
18+
public ListApplicationRepository(ApplicationContext db)
19+
{
20+
_db = db;
21+
}
22+
23+
public async Task<IEnumerable<Application>> ListApplicationsByOwner(string ownerId)
24+
{
25+
return await _db.Applications.AsNoTracking().Where(x => x.User == ownerId).ToListAsync();
26+
}
27+
28+
public async Task<Application> FindById(string id)
29+
{
30+
return await _db.Applications.AsNoTracking().FirstOrDefaultAsync(x => x.Id == id);
31+
}
32+
}
33+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Micro.AppRegistration.Api.Models;
6+
7+
namespace Micro.AppRegistration.Api.ListApplications
8+
{
9+
public interface IListApplicationService
10+
{
11+
public Task<IEnumerable<ApplicationResponse>> FindByOwner(string ownerId);
12+
public Task<ApplicationResponse> FindByAppId(string appId, string userId);
13+
}
14+
public class ListApplicationsService : IListApplicationService
15+
{
16+
private readonly IListApplicationRepository _repository;
17+
18+
private static ApplicationResponse MapApplicationResponse(Application application)
19+
{
20+
return new ApplicationResponse
21+
{
22+
Approved = application.Approved,
23+
Id = application.Id,
24+
Name = application.Name,
25+
User = application.User,
26+
CreatedAt = application.CreatedAt,
27+
CreatedBy = application.CreatedBy,
28+
ShortCode = application.ShortCode,
29+
UseDefaultCode = application.UseDefaultCode
30+
};
31+
}
32+
public ListApplicationsService(IListApplicationRepository repository)
33+
{
34+
_repository = repository;
35+
}
36+
37+
public async Task<IEnumerable<ApplicationResponse>> FindByOwner(string ownerId)
38+
{
39+
var applications = await _repository.ListApplicationsByOwner(ownerId);
40+
return applications.Select(MapApplicationResponse);
41+
}
42+
43+
public async Task<ApplicationResponse> FindByAppId(string appId, string ownerId)
44+
{
45+
var app = await _repository.FindById(appId);
46+
if (ownerId != app.User)
47+
{
48+
throw new UnauthorizedRequestException();
49+
}
50+
return MapApplicationResponse(app);
51+
}
52+
}
53+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace Micro.AppRegistration.Api.ListApplications
4+
{
5+
public class UnauthorizedRequestException : Exception
6+
{
7+
8+
}
9+
}

Micro.AppRegistration.Api/Micro.AppRegistration.Api.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc2" />
2121
</ItemGroup>
2222

23-
<ItemGroup>
24-
<Folder Include="Repository" />
25-
</ItemGroup>
26-
2723
<Target Name="Install githooks" BeforeTargets="Build">
2824
<Exec Command="cp ../hooks/* ../.git/hooks/" />
2925
</Target>

Micro.AppRegistration.Api/StartupExtensions/DependencyInjection.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Micro.AppRegistration.Api.Auth;
44
using Micro.AppRegistration.Api.Configs;
55
using Micro.AppRegistration.Api.CreateApplication;
6+
using Micro.AppRegistration.Api.ListApplications;
67
using Micro.AppRegistration.Api.Models;
78
using Micro.AppRegistration.Api.Uuid;
89
using Microsoft.AspNetCore.Identity;
@@ -20,6 +21,8 @@ public static void ConfigureRequiredDependencies(this IServiceCollection service
2021
services.AddSingleton<IKeyResolver, KeyResolver>();
2122
services.AddScoped<IAppRegistrationService, AppRegistrationService>();
2223
services.AddScoped<ICreateApplicationRepository, CreateApplicationRepository>();
24+
services.AddScoped<IListApplicationRepository, ListApplicationRepository>();
25+
services.AddScoped<IListApplicationService, ListApplicationsService>();
2326
services.AddSingleton<IPasswordHasher<Application>, PasswordHasher<Application>>();
2427
services.AddSingleton(SetupKeyStoreHttpClient(configuration.GetSection("Services").Get<Services>().KeyStore));
2528
}

0 commit comments

Comments
 (0)