Skip to content

Commit

Permalink
Merge pull request #8 from BirajMainali/chore/app
Browse files Browse the repository at this point in the history
Chore/app
  • Loading branch information
BirajMainali authored Jan 24, 2024
2 parents a943f69 + 4b1a9c7 commit 15eddf8
Show file tree
Hide file tree
Showing 59 changed files with 1,265 additions and 243 deletions.
3 changes: 3 additions & 0 deletions App.Base/Configurations/IScopedDependency.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace App.Base.Configurations;

public interface IScopedDependency;
3 changes: 3 additions & 0 deletions App.Base/Configurations/ITransientDependency.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace App.Base.Configurations;

public interface ITransientDependency;
7 changes: 7 additions & 0 deletions App.Base/Constants/AuthenticationKeyConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace App.Base.Constants;

public class AuthenticationKeyConstants
{
public const string AuthenticationKey = "damn-unique-authentication-key-x";
public const string MultiTenantAuthenticationKey = "damn-unique-multi-tenant-authentication-key-x";
}
7 changes: 7 additions & 0 deletions App.Base/Constants/ClaimsConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace App.Base.Constants;

public static class ClaimsConstants
{
public static string ProtectedClaimsKey = "protected-extra-claims";
public static string ProtectedClaimsHttpClaimKey = "protected-extra-claims-http-claim";
}
4 changes: 2 additions & 2 deletions App.Base/DataContext/Interfaces/IUow.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using App.Base.Configurations;
using Microsoft.EntityFrameworkCore;

namespace App.Base.DataContext.Interfaces
{
public interface IUow
public interface IUow : IScopedDependency
{
DbContext Context { get; }
void Commit();
Expand All @@ -15,6 +16,5 @@ public interface IUow
void UpdateRange<T>(IEnumerable<T> list) where T : class;
void Remove<T>(T entity);
void RemoveRange<T>(IEnumerable<T> list) where T : class;

}
}
22 changes: 0 additions & 22 deletions App.Base/DiConfig.cs

This file was deleted.

9 changes: 4 additions & 5 deletions App.Base/Entities/FullAuditedEntity.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#nullable enable
using System;
using System;
using App.Base.Constants;

namespace App.Base.Entities
{
public abstract class FullAuditedEntity<TKey>
{
public TKey Id { get; set; }
public DateTime CreatedDate { get; set; } = DateTime.Now;
public DateTime CreatedDate { get; set; } = DateTime.UtcNow;
public DateTime? UpdatedDate { get; set; }
public char RecStatus { get; set; } = Status.Active;
public TKey CreatedBy { get; set; }
public TKey UpdatedBy { get; set; }
public string? CreatedBy { get; set; }
public string? UpdatedBy { get; set; }
}
}
4 changes: 2 additions & 2 deletions App.Base/Extensions/ApiControllerExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace App.Base.Extensions
{
public static class ApiControllerExtension
{
public static IActionResult SendSuccess(this Controller controller, string notify, object data = null)
public static IActionResult SendSuccess(this ControllerBase controller, string notify, object data = null)
{
return controller.Ok(new
{
Expand All @@ -13,7 +13,7 @@ public static IActionResult SendSuccess(this Controller controller, string notif
});
}

public static IActionResult SendError(this Controller controller, string error)
public static IActionResult SendError(this ControllerBase controller, string error)
{
return controller.BadRequest(new
{
Expand Down
14 changes: 14 additions & 0 deletions App.Base/Extensions/JsonExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Text.Json;

namespace App.Base.Extensions;

public static class JsonExtension
{
public static string ToJson<T>(this T value, Action<JsonSerializerOptions>? options = null)
{
var newOptions = new JsonSerializerOptions();
options?.Invoke(newOptions);
return JsonSerializer.Serialize(value, newOptions);
}
}
35 changes: 32 additions & 3 deletions App.Base/Extensions/StringExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Text.RegularExpressions;
using System.Linq;

namespace App.Base.Extensions
{
Expand All @@ -11,8 +11,37 @@ public static string ToSnakeCase(this string input)
return input;
}

var startUnderscores = Regex.Match(input, @"^_+");
return startUnderscores + Regex.Replace(input, @"([a-z0-9])([A-Z])", "$1_$2").ToLower();
var snakeCase = string.Concat(input.Select((x, i) =>
{
if (i > 0 && char.IsUpper(x))
{
return "_" + char.ToLower(x);
}
else if (char.IsWhiteSpace(x))
{
return "_";
}

return char.ToLower(x).ToString();
}));

return snakeCase;
}

public static string IgnoreCase(this string str)
{
return str.Trim().ToLower();
}

public static string Or(this string str, string or)
{
return string.IsNullOrEmpty(str) ? or : str;
}

public static string ValueOrProvided(this string str, string defaultValue = "")
=> string.IsNullOrEmpty(str) ? defaultValue : str;

public static string ValueOrNull(this string str)
=> str.ValueOrProvided(null);
}
}
2 changes: 1 addition & 1 deletion App.Base/Helper/FileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static async Task<string> SavePhysicalFile(FileRecordVo recordVo)
var extension = Path.GetExtension(recordVo.File.FileName);
var encryptedFileName = Guid.NewGuid() + extension;
var filePath = Path.Combine(path, encryptedFileName);
await using var stream = new FileStream(filePath, FileMode.Create);
await using var stream = new FileStream(filePath, FileMode.Create);
await recordVo.File.CopyToAsync(stream);
return encryptedFileName;
}
Expand Down
25 changes: 0 additions & 25 deletions App.Base/Providers/ConnectionProvider.cs

This file was deleted.

38 changes: 38 additions & 0 deletions App.Base/Providers/DatabaseConnectionProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using App.Base.Providers.Interface;
using App.Base.Settings;
using Microsoft.Extensions.Options;
using Npgsql;

namespace App.Base.Providers
{
public class DatabaseConnectionProvider : IDatabaseConnectionProvider
{
private readonly IOptions<AppSettings> _appSettings;

public DatabaseConnectionProvider(IOptions<AppSettings> appSettings)
{
_appSettings = appSettings;
}

/// <summary>
/// Ready to use NpgsqlConnection
/// </summary>
/// <returns></returns>
public NpgsqlConnection GetConnection()
{
var defaultConnection = _appSettings.Value.ConnectionStrings.DefaultConnection;
return new NpgsqlConnection(defaultConnection);
}

public string GetConnectionString(string databaseName)
{
var defaultConnection = _appSettings.Value.ConnectionStrings.DefaultConnection;
var builder = new NpgsqlConnectionStringBuilder(defaultConnection)
{
Database = databaseName
};
return builder.ConnectionString;
}
}
}
9 changes: 0 additions & 9 deletions App.Base/Providers/Interface/IConnectionProvider.cs

This file was deleted.

11 changes: 11 additions & 0 deletions App.Base/Providers/Interface/IDatabaseConnectionProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using App.Base.Configurations;
using Npgsql;

namespace App.Base.Providers.Interface
{
public interface IDatabaseConnectionProvider : IScopedDependency
{
NpgsqlConnection GetConnection();
string GetConnectionString(string databaseName);
}
}
2 changes: 1 addition & 1 deletion App.Base/Repository/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ public interface IRepository<T, in TKey> where T : class

IQueryable<T?> GetQueryable();

Task<T> FindOrThrowAsync(TKey id, string message);
Task<T> FindOrThrowAsync(TKey id);
}
12 changes: 9 additions & 3 deletions App.Base/Repository/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,20 @@ public virtual Task<List<T>> GetAllAsync(Expression<Func<T, bool>> predicate = n

public virtual IQueryable<T?> GetQueryable() => _dbSet.AsQueryable();

public virtual async Task<T> FindOrThrowAsync(TKey id, string message)
public async Task<T> FindOrThrowAsync(TKey id, Func<T?, Exception> exception)
{
var entity = await FindByAsync(id);
if (entity == null) throw exception(entity);
return entity;
}

public async Task<T> FindOrThrowAsync(TKey id)
{
var entity = await FindByAsync(id);
if (entity == null)
{
throw new Exception(message);
throw new KeyNotFoundException($"Entity with key {id} was not found.");
}

return entity;
}
}
14 changes: 14 additions & 0 deletions App.Base/Settings/AppSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace App.Base.Settings
{
public class AppSettings
{
public ConnectionStrings ConnectionStrings { get; set; }
public bool UseMultiTenancy { get; set; } = false;
public string DefaultDataProtectionPurpose { get; set; }
}

public class ConnectionStrings
{
public string DefaultConnection { get; set; }
}
}
1 change: 1 addition & 0 deletions App.User/App.User.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" Version="8.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
14 changes: 0 additions & 14 deletions App.User/DiConfig.cs

This file was deleted.

11 changes: 11 additions & 0 deletions App.User/Entity/AppUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@ namespace App.User.Entity;

public class AppUser : FullAuditedEntity<long>
{
public virtual ApplicationTenant? Tenant { get; protected set; }
public long? TenantId { get; protected set; }
public string Name { get; protected set; }
public string Gender { get; protected set; }
public string Email { get; protected set; }
public string Password { get; protected set; }
public string Address { get; protected set; }
public string Phone { get; protected set; }

public virtual AppUser? ParentUser { get; protected set; }
public long? ParentUserId { get; protected set; }

public AppUser SetTenant(ApplicationTenant tenant)
{
Tenant = tenant;
return this;
}

public AppUser()
{
}
Expand Down
21 changes: 21 additions & 0 deletions App.User/Entity/ApplicationTenant.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using App.Base.Entities;

namespace App.User.Entity;

public class ApplicationTenant : FullAuditedEntity<Guid>
{
public string Name { get; protected set; }
public string DatabaseName { get; protected set; }

public ApplicationTenant()
{
}

public ApplicationTenant(string name, string databaseName)
=> Copy(name, databaseName);
private void Copy(string name, string databaseName)
{
Name = name;
DatabaseName = databaseName;
}
}
Loading

0 comments on commit 15eddf8

Please sign in to comment.