initial commit
This commit is contained in:
94
App_Code/Controller/APIController.cs
Normal file
94
App_Code/Controller/APIController.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Library;
|
||||
|
||||
public class APIController : AbstractController
|
||||
{
|
||||
protected APIController( ILogger logger , IConfiguration configuration )
|
||||
: base( logger , configuration )
|
||||
{
|
||||
}
|
||||
|
||||
protected void AssertID( int value )
|
||||
{
|
||||
if ( value <= 0 )
|
||||
throw new APIErrorException( "Invalid id parameter" );
|
||||
}
|
||||
|
||||
protected void AssertParameter( int value , int min_value )
|
||||
{
|
||||
if ( value < min_value )
|
||||
throw new APIErrorException( "Invalid int parameter" );
|
||||
}
|
||||
|
||||
protected void AssertParameter( string? value , int min_length )
|
||||
{
|
||||
if ( !ValidateString( value , min_length ) )
|
||||
throw new APIErrorException( "Invalid string parameter" );
|
||||
}
|
||||
|
||||
protected void AssertURL( string? value , bool mandatory )
|
||||
{
|
||||
if ( !ValidateURL( value , mandatory ) )
|
||||
throw new APIErrorException( "Invalid url parameter" );
|
||||
}
|
||||
|
||||
protected void AssertEmail( string? value , bool mandatory )
|
||||
{
|
||||
if ( !ValidateEmail( value , mandatory ) )
|
||||
throw new APIErrorException( "Invalid email parameter" );
|
||||
}
|
||||
|
||||
protected override void OnInvalidLogin( )
|
||||
{
|
||||
throw new APIErrorException( "Access denied." );
|
||||
}
|
||||
|
||||
protected override void OnInvalidAdminLogin( )
|
||||
{
|
||||
throw new APIErrorException( "Access denied (admin)." );
|
||||
}
|
||||
|
||||
protected ContentResult Error( string message )
|
||||
{
|
||||
using MemoryStream stream = new( );
|
||||
|
||||
using Utf8JsonWriter writer = new( stream );
|
||||
writer.WriteStartObject( );
|
||||
writer.WriteBoolean( "Success" , false );
|
||||
writer.WriteString( "ErrorMessage" , message );
|
||||
writer.WriteEndObject( );
|
||||
writer.Dispose( );
|
||||
|
||||
return new ContentResult( )
|
||||
{
|
||||
ContentType = System.Net.Mime.MediaTypeNames.Application.Json ,
|
||||
Content = Encoding.UTF8.GetString( stream.ToArray( ) )
|
||||
};
|
||||
}
|
||||
|
||||
protected ContentResult Success( )
|
||||
{
|
||||
return Success( "true" );
|
||||
}
|
||||
|
||||
protected ContentResult Success( string data )
|
||||
{
|
||||
using MemoryStream stream = new( );
|
||||
|
||||
using Utf8JsonWriter writer = new( stream );
|
||||
writer.WriteStartObject( );
|
||||
writer.WriteBoolean( "Success" , true );
|
||||
writer.WriteString( "Result" , data );
|
||||
writer.WriteEndObject( );
|
||||
writer.Dispose( );
|
||||
|
||||
return new ContentResult( )
|
||||
{
|
||||
ContentType = System.Net.Mime.MediaTypeNames.Application.Json ,
|
||||
Content = Encoding.UTF8.GetString( stream.ToArray( ) )
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user