Skip to content

Commit

Permalink
Merge pull request #49 from HoodDigital/api
Browse files Browse the repository at this point in the history
Added api project, startup and base controllers.
  • Loading branch information
JorjeRedemption authored Oct 26, 2022
2 parents 1dbfabe + 70b4875 commit 294bdc5
Show file tree
Hide file tree
Showing 24 changed files with 548 additions and 111 deletions.
23 changes: 23 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@
"sourceFileMap": {
"/Views": "${workspaceFolder}/projects/Hood.Development/Views"
}
},
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": "Hood.Api",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-dev",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/projects/Hood.Api/bin/Debug/net6.0/Hood.Api.dll",
"args": [],
"cwd": "${workspaceFolder}/projects/Hood.Api/",
"stopAtEntry": false,
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_URLS": "https://localhost:5154"
},
}
]
}
21 changes: 21 additions & 0 deletions Hood.sln
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hood.Development", "project
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hood.Admin", "projects\Hood.Admin\Hood.Admin.csproj", "{123F61E6-834C-42FF-B62E-12CE30BC8A90}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "projects", "projects", "{9A9C95A8-97A7-441E-9156-4DE6455755F8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hood.Api", "projects\Hood.Api\Hood.Api.csproj", "{676E5E20-C394-4A10-BF4B-25A177FD4E85}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -162,6 +166,22 @@ Global
{123F61E6-834C-42FF-B62E-12CE30BC8A90}.Release|x64.Build.0 = Release|Any CPU
{123F61E6-834C-42FF-B62E-12CE30BC8A90}.Release|x86.ActiveCfg = Release|Any CPU
{123F61E6-834C-42FF-B62E-12CE30BC8A90}.Release|x86.Build.0 = Release|Any CPU
{676E5E20-C394-4A10-BF4B-25A177FD4E85}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{676E5E20-C394-4A10-BF4B-25A177FD4E85}.Debug|Any CPU.Build.0 = Debug|Any CPU
{676E5E20-C394-4A10-BF4B-25A177FD4E85}.Debug|ARM.ActiveCfg = Debug|Any CPU
{676E5E20-C394-4A10-BF4B-25A177FD4E85}.Debug|ARM.Build.0 = Debug|Any CPU
{676E5E20-C394-4A10-BF4B-25A177FD4E85}.Debug|x64.ActiveCfg = Debug|Any CPU
{676E5E20-C394-4A10-BF4B-25A177FD4E85}.Debug|x64.Build.0 = Debug|Any CPU
{676E5E20-C394-4A10-BF4B-25A177FD4E85}.Debug|x86.ActiveCfg = Debug|Any CPU
{676E5E20-C394-4A10-BF4B-25A177FD4E85}.Debug|x86.Build.0 = Debug|Any CPU
{676E5E20-C394-4A10-BF4B-25A177FD4E85}.Release|Any CPU.ActiveCfg = Release|Any CPU
{676E5E20-C394-4A10-BF4B-25A177FD4E85}.Release|Any CPU.Build.0 = Release|Any CPU
{676E5E20-C394-4A10-BF4B-25A177FD4E85}.Release|ARM.ActiveCfg = Release|Any CPU
{676E5E20-C394-4A10-BF4B-25A177FD4E85}.Release|ARM.Build.0 = Release|Any CPU
{676E5E20-C394-4A10-BF4B-25A177FD4E85}.Release|x64.ActiveCfg = Release|Any CPU
{676E5E20-C394-4A10-BF4B-25A177FD4E85}.Release|x64.Build.0 = Release|Any CPU
{676E5E20-C394-4A10-BF4B-25A177FD4E85}.Release|x86.ActiveCfg = Release|Any CPU
{676E5E20-C394-4A10-BF4B-25A177FD4E85}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -174,6 +194,7 @@ Global
{C8D14AA4-199E-409F-B02D-4B2818B8DE0B} = {5CF84357-EA5D-42CF-8CBC-05940FF0F73C}
{7ADB2A9E-146D-40C4-826A-C79C712CE905} = {306ED7A4-6F3F-4BE1-96CE-CA9DAE98CC53}
{123F61E6-834C-42FF-B62E-12CE30BC8A90} = {A89DE859-75A0-418B-BE31-3228BD827619}
{676E5E20-C394-4A10-BF4B-25A177FD4E85} = {9A9C95A8-97A7-441E-9156-4DE6455755F8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E5B47CFC-A139-40DC-9B6A-7CB4C3ED7C71}
Expand Down
2 changes: 1 addition & 1 deletion projects/Hood.Admin/Hood.Admin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<AssemblyName>Hood.Admin</AssemblyName>
<PackageId>Hood.Admin</PackageId>

<Version>6.1.3</Version>
<Version>6.1.4</Version>
<TargetFramework>net6.0</TargetFramework>
<Authors>Hood Digital;George Whysall;</Authors>
<OutputType>Library</OutputType>
Expand Down
23 changes: 23 additions & 0 deletions projects/Hood.Api/Controllers/ContentController.cs
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()
{ }
}
}
15 changes: 15 additions & 0 deletions projects/Hood.Api/Controllers/HomeController.cs
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()
{ }
}
}
14 changes: 14 additions & 0 deletions projects/Hood.Api/Hood.Api.csproj
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>
27 changes: 27 additions & 0 deletions projects/Hood.Api/Program.cs
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>();
});

}
}
53 changes: 53 additions & 0 deletions projects/Hood.Api/Startup.cs
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();
});
}

}

}
66 changes: 66 additions & 0 deletions projects/Hood.Api/appsettings.json
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 projects/Hood.Core/BaseControllers/Api/ContentController.cs
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
}
});
}

}
}
46 changes: 46 additions & 0 deletions projects/Hood.Core/BaseControllers/Api/HomeController.cs
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);
}

}
}
Loading

0 comments on commit 294bdc5

Please sign in to comment.