Skip to content

Commit

Permalink
Controller helper
Browse files Browse the repository at this point in the history
  • Loading branch information
KennyMack committed Nov 23, 2023
1 parent b7550be commit 76b5f17
Show file tree
Hide file tree
Showing 28 changed files with 1,395 additions and 2 deletions.
32 changes: 30 additions & 2 deletions NetSwissTools.Web.sln
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34031.279
# Visual Studio Version 16
VisualStudioVersion = 16.0.34031.81
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Test", "Test", "{E938E22F-DEA2-42A9-A2DC-F81A555BB1FF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tools", "Tools", "{8C65E219-42F9-41FC-ADFB-4938C1DB2E51}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetSwissTools.Web", "src\NetSwissTools.Web\NetSwissTools.Web.csproj", "{58742414-637E-4AAA-8FC4-A8C7C93FAE49}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{A82929A9-88AB-4C42-A160-4AC238D4528F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Clients.API", "samples\Clients.API\Clients.API.csproj", "{117B883A-1C2C-46AE-880B-403B2609EC0B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{58742414-637E-4AAA-8FC4-A8C7C93FAE49}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{58742414-637E-4AAA-8FC4-A8C7C93FAE49}.Debug|Any CPU.Build.0 = Debug|Any CPU
{58742414-637E-4AAA-8FC4-A8C7C93FAE49}.Release|Any CPU.ActiveCfg = Release|Any CPU
{58742414-637E-4AAA-8FC4-A8C7C93FAE49}.Release|Any CPU.Build.0 = Release|Any CPU
{117B883A-1C2C-46AE-880B-403B2609EC0B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{117B883A-1C2C-46AE-880B-403B2609EC0B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{117B883A-1C2C-46AE-880B-403B2609EC0B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{117B883A-1C2C-46AE-880B-403B2609EC0B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{58742414-637E-4AAA-8FC4-A8C7C93FAE49} = {8C65E219-42F9-41FC-ADFB-4938C1DB2E51}
{117B883A-1C2C-46AE-880B-403B2609EC0B} = {A82929A9-88AB-4C42-A160-4AC238D4528F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9BB015AE-CDF3-415F-94FF-B957B290C771}
EndGlobalSection
Expand Down
10 changes: 10 additions & 0 deletions samples/Clients.API/Client.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Clients.API
{
public class Client
{
public Guid Id { get; set; }
public string Name { get; set; }

Check warning on line 6 in samples/Clients.API/Client.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 6 in samples/Clients.API/Client.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public int Age { get; set; }
public string Email { get; set; }

Check warning on line 8 in samples/Clients.API/Client.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Email' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 8 in samples/Clients.API/Client.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Email' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
}
18 changes: 18 additions & 0 deletions samples/Clients.API/Clients.API.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\NetSwissTools.Web\NetSwissTools.Web.csproj" />
</ItemGroup>

</Project>
81 changes: 81 additions & 0 deletions samples/Clients.API/Controllers/ClientsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using Microsoft.AspNetCore.Mvc;
using NetSwissTools.Web.Mvc;
using NetSwissTools.Web.Mvc.Results;
using System;
using System.Net;
using System.Runtime.CompilerServices;

namespace Clients.API.Controllers
{
[ApiController]
[Route("[controller]")]
[ApiVersion("1")]
public class ClientsController : SwissControllerApi
{
public ClientsController(ILogger<ClientsController> logger)
{
}

[HttpPost]
public async Task<IActionResult> Post(Client client)
{
lock (MemoryStore.Clients)
{
client.Id = Guid.NewGuid();

MemoryStore.Clients.Add(client);
}
await Task.Delay(1000);
return base.Created(client.Id.ToString(), client);
}

[HttpPost("Pending")]
public async Task<IActionResult> PostCreate(Client client)
{
lock (MemoryStore.ClientsPending)
{
client.Id = Guid.NewGuid();

MemoryStore.ClientsPending.Add(client);
}
await Task.Delay(1000);
return Created(client.Id.ToString(), client);
}

[HttpPost("Simulate/{id}")]
public async Task<IActionResult> PostSimulate(Guid id)
{
await Task.Delay(1000);
lock (MemoryStore.ClientsPending)
{
var client = MemoryStore.ClientsPending.FirstOrDefault(x => x.Id == id);

if (client == null)
return MovedPermanently(id.ToString());

MemoryStore.Clients.Add(client);
MemoryStore.ClientsPending.Remove(client);

return Created(client.Id.ToString(), client);
}
}

[HttpGet]
public ActionResult<Client> Get()
{
lock (MemoryStore.Clients)
{
return RequestOK(MemoryStore.Clients);
}
}

[HttpGet("{id}")]
public ActionResult<Client> GetById(Guid id)
{
lock (MemoryStore.Clients)
{
return RequestOK(MemoryStore.Clients.Find(x => x.Id == id));
}
}
}
}
8 changes: 8 additions & 0 deletions samples/Clients.API/MemoryStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Clients.API
{
public static class MemoryStore
{
public static List<Client> Clients = new();
public static List<Client> ClientsPending = new();
}
}
25 changes: 25 additions & 0 deletions samples/Clients.API/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
31 changes: 31 additions & 0 deletions samples/Clients.API/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:38011",
"sslPort": 44392
}
},
"profiles": {
"Clients.API": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7038;http://localhost:5031",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
8 changes: 8 additions & 0 deletions samples/Clients.API/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions samples/Clients.API/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
18 changes: 18 additions & 0 deletions src/NetSwissTools.Web/Enums/EExceptionErrorCodes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace NetSwissTools.Web.Enums
{
public enum EExceptionErrorCodes
{
UnhandledException = 900,
InvalidRequest = 997,
UnauthorizedRequest = 998,
InvalidEstablishmentKey = 999,
ValidationError = -10000,
RegisterNotFound = -15000,
RegisterChild = -16000,
InsertSQLError = -20001,
UpdateSQLError = -20002,
DeleteSQLError = -20003,
SaveSQLError = -20004,
SQLCommandError = -20005
}
}
32 changes: 32 additions & 0 deletions src/NetSwissTools.Web/Mvc/Helpers/ResultStatusHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Net;

namespace NetSwissTools.Web.Mvc.Helpers
{
public static class ResultStatusHelper
{
public static bool IsClientError(HttpStatusCode code) =>
(int)code >= 400 && (int)code <= 499;

public static bool IsInformational(HttpStatusCode code) =>
(int)code >= 100 && (int)code <= 199;

public static bool IsRedirect(HttpStatusCode code) =>
(int)code >= 300 && (int)code <= 399;

public static bool IsServerError(HttpStatusCode code) =>
(int)code >= 500 && (int)code <= 599;

public static bool IsSuccess(HttpStatusCode code) =>
(int)code >= 200 && (int)code <= 299;

public static bool IsSuccessReponse(HttpStatusCode code)
{
if (IsInformational(code) || IsSuccess(code) || IsRedirect(code))
return true;
else if (IsClientError(code) || IsServerError(code))
return false;

return false;
}
}
}
9 changes: 9 additions & 0 deletions src/NetSwissTools.Web/Mvc/Interfaces/IErrorService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using NetSwissTools.Exceptions;

namespace NetSwissTools.Web.Mvc.Interfaces
{
public interface IErrorService
{
List<ModelException> Errors { get; set; }
}
}
20 changes: 20 additions & 0 deletions src/NetSwissTools.Web/Mvc/Interfaces/IErrorsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using NetSwissTools.Exceptions;

namespace NetSwissTools.Web.Mvc.Interfaces
{
public interface IErrorsController
{
void AddError(string message);
void AddError(string field, string exception);
void AddError(string field, string exception, string value);
void AddError(int code, string field, string exception, string value);
void AddError(ModelException exception);
void AddError(ModelException[] exceptions);
void AddError(Exception exception);
void ClearErrors();
ModelException[] GetErrors();
bool HasAnyErrors();
bool IsOperationValid();
bool ValidateModelState<TEntity>(TEntity pModel) where TEntity : class;
}
}
15 changes: 15 additions & 0 deletions src/NetSwissTools.Web/Mvc/Interfaces/IResultStatusController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Net;

namespace NetSwissTools.Web.Mvc.Interfaces
{
public interface IResultStatusController
{
bool IsInformational(HttpStatusCode code);
bool IsSuccess(HttpStatusCode code);
bool IsRedirect(HttpStatusCode code);
bool IsClientError(HttpStatusCode code);
bool IsServerError(HttpStatusCode code);
bool IsSuccessReponse(HttpStatusCode code);

}
}
13 changes: 13 additions & 0 deletions src/NetSwissTools.Web/Mvc/Interfaces/IUrlInfoController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace NetSwissTools.Web.Mvc.Interfaces
{
public interface IUrlInfoController
{
IQueryCollection QueryString { get; }
int GetPageNumber();
int GetPageSize();
string GetSort();
string GetFilter();
string GetQueryColumn();
DateTime? GetDateTime(string pDate);
}
}
Loading

0 comments on commit 76b5f17

Please sign in to comment.