-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from HoodDigital/api
Added api project, startup and base controllers.
- Loading branch information
Showing
24 changed files
with
548 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Threading; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Cors; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Hood.Models; | ||
using Hood.ViewModels; | ||
using Hood.Services; | ||
using Hood.Contexts; | ||
using Hood.Caching; | ||
using Hood.Core; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json; | ||
|
||
namespace Hood.Api.Controllers | ||
{ | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class ContentController : Hood.Api.BaseControllers.ContentController | ||
{ | ||
public ContentController() : base() | ||
{ } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Threading; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Cors; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Hood.Models; | ||
|
||
namespace Hood.Api.Controllers | ||
{ | ||
[ApiController] | ||
public class HomeController : Hood.Api.BaseControllers.HomeController | ||
{ | ||
public HomeController() : base() | ||
{ } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<UserSecretsId>Hood.Web</UserSecretsId> | ||
<Copyright>Hood Digital Limited</Copyright> | ||
<TypeScriptToolsVersion>4.3</TypeScriptToolsVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Hood.Core\Hood.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Threading.Tasks; | ||
using Hood.Startup; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace Hood.Api | ||
{ | ||
public class Program | ||
{ | ||
public static async Task Main(string[] args) | ||
{ | ||
var builder = await CreateHostBuilder(args) | ||
.Build() | ||
.LoadHoodAsync(); | ||
|
||
builder.Run(); | ||
} | ||
|
||
public static IHostBuilder CreateHostBuilder(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.ConfigureWebHostDefaults(webBuilder => | ||
{ | ||
webBuilder.UseStartup<Startup>(); | ||
}); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
| ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
using Microsoft.AspNetCore.Http; | ||
using Newtonsoft.Json; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Hosting; | ||
using Hood.Startup; | ||
using Hood.Models; | ||
|
||
namespace Hood.Api | ||
{ | ||
public class Startup | ||
{ | ||
public IConfiguration Configuration { get; } | ||
public IWebHostEnvironment Environment { get; } | ||
|
||
public Startup(IConfiguration configuration, IWebHostEnvironment env) | ||
{ | ||
this.Configuration = configuration; | ||
this.Environment = env; | ||
} | ||
|
||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.ConfigureHoodApi(Configuration, Environment); | ||
} | ||
|
||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | ||
{ | ||
if (env.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
} | ||
|
||
app.UseRouting(); | ||
app.UseCors(); | ||
|
||
app.UseAuthentication(); | ||
app.UseAuthorization(); | ||
|
||
app.UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapControllers(); | ||
}); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
{ | ||
"ConnectionStrings": { | ||
"DefaultConnection": "Server=localhost\\SQLEXPRESS;Database=Hood.Web;Trusted_Connection=True;MultipleActiveResultSets=true;" | ||
}, | ||
"Hood": { | ||
"SuperAdminEmail": "[email protected]", | ||
"InitializeOnStartup": false, | ||
"ApplyMigrationsAutomatically": true, | ||
"LogLevel": "None", | ||
"BypassCDN": true, | ||
"Integrations": { | ||
"TinyMCE": "no-api-key", | ||
"Google": { | ||
"ApiKey": "", | ||
"EnableMaps": true, | ||
"EnableGeocoding": true, | ||
"Recaptcha": { | ||
"Enabled": true, | ||
"Key": "", | ||
"Secret": "" | ||
} | ||
}, | ||
"SendGrid": { | ||
"ApiKey": "" | ||
}, | ||
"SMTP": { | ||
"Server": "", | ||
"Port": "", | ||
"Username": "", | ||
"Password": "" | ||
} | ||
} | ||
}, | ||
"Identity": { | ||
"Auth0": { | ||
"Domain": "", | ||
"ClientId": "", | ||
"ClientSecret": "", | ||
"SetupRemoteOnIntitialize": false | ||
}, | ||
"Password": { | ||
"RequireDigit": true, | ||
"RequireLowercase": true, | ||
"RequireUppercase": false, | ||
"RequireNonAlphanumeric": false, | ||
"RequiredLength": 6 | ||
}, | ||
"Cookies": { | ||
"Name": "Hood", | ||
"ConsentRequired": true | ||
} | ||
}, | ||
"Cors": { | ||
"Domains":[ | ||
"localhost:5152", | ||
"localhost:5153" | ||
] | ||
}, | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
projects/Hood.Core/BaseControllers/Api/ContentController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Threading; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Cors; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Hood.Models; | ||
using Hood.ViewModels; | ||
using Hood.Services; | ||
using Hood.Contexts; | ||
using Hood.Caching; | ||
using Hood.Core; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json; | ||
|
||
namespace Hood.Api.BaseControllers | ||
{ | ||
[ApiController] | ||
[Route("[controller]")] | ||
public abstract class ContentController : Controller | ||
{ | ||
protected readonly ContentContext _contentDb; | ||
protected readonly IContentRepository _content; | ||
protected readonly ContentCategoryCache _contentCategoryCache; | ||
|
||
public ContentController() | ||
: base() | ||
{ | ||
_contentDb = Engine.Services.Resolve<ContentContext>(); | ||
_content = Engine.Services.Resolve<IContentRepository>(); | ||
_contentCategoryCache = Engine.Services.Resolve<ContentCategoryCache>(); | ||
} | ||
|
||
[HttpGet("")] | ||
public async Task<IActionResult> IndexAsync(string type, int pageIndex = 1, int pageSize = 20) | ||
{ | ||
var model = await _content.GetContentAsync(new ContentModel() | ||
{ | ||
Type = type, | ||
PageIndex = pageIndex, | ||
PageSize = pageSize | ||
}); | ||
return Json(new | ||
{ | ||
data = model.List, | ||
pagination = new | ||
{ | ||
pageIndex, | ||
pageSize, | ||
count = model.TotalCount, | ||
totalPages = model.TotalPages | ||
} | ||
}); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Threading; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Cors; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Hood.Models; | ||
|
||
namespace Hood.Api.BaseControllers | ||
{ | ||
[ApiController] | ||
public abstract class HomeController : ControllerBase | ||
{ | ||
public HomeController() : base() | ||
{ } | ||
|
||
private const string publicMessage = "The API doesn't require an access token to share this message."; | ||
private const string protectedMessage = "The API successfully validated your access token."; | ||
private const string adminMessage = "The API successfully recognized you as an admin."; | ||
|
||
[HttpGet("")] | ||
public string Index() | ||
{ | ||
return $"Hood API."; | ||
} | ||
|
||
[HttpGet("public")] | ||
public ApiResponse GetPublicMessage() | ||
{ | ||
return new ApiResponse(publicMessage); | ||
} | ||
|
||
[HttpGet("protected")] | ||
[Authorize] | ||
public ApiResponse GetProtectedMessage() | ||
{ | ||
return new ApiResponse(protectedMessage); | ||
} | ||
|
||
[HttpGet("admin")] | ||
[Authorize(Policy = "Admin")] | ||
public ApiResponse GetAdminMessage() | ||
{ | ||
return new ApiResponse(adminMessage); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.