Skip to content

Commit

Permalink
Code style refactor (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
lanedirt committed Jun 12, 2024
1 parent 1568427 commit d456932
Show file tree
Hide file tree
Showing 16 changed files with 410 additions and 171 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,5 @@ dotnet_naming_style.pascal_case.capitalization = pascal_case
dotnet_style_operator_placement_when_wrapping = beginning_of_line
tab_width = 4
end_of_line = crlf
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
13 changes: 13 additions & 0 deletions SonarLint.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<SonarLint>
<Rules>
<Rule>
<Key>S1135</Key>
<Parameters>
<Parameter>
<Name>sonarlint.rule.enabled</Name>
<Value>false</Value>
</Parameter>
</Parameters>
</Rule>
</Rules>
</SonarLint>
4 changes: 2 additions & 2 deletions src/AliasDb/Identity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public class Identity
/// </summary>
[StringLength(255)]
[Column(TypeName = "VARCHAR")]
public string FirstName { get; set; } = null!;
public string? FirstName { get; set; } = null!;

/// <summary>
/// Gets or sets the last name.
/// </summary>
[StringLength(255)]
[Column(TypeName = "VARCHAR")]
public string LastName { get; set; } = null!;
public string? LastName { get; set; } = null!;

/// <summary>
/// Gets or sets the nickname.
Expand Down
116 changes: 64 additions & 52 deletions src/AliasVault.Api/Controllers/AliasController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace AliasVault.Api.Controllers;

using System.Globalization;
using AliasDb;
using AliasVault.Shared.Models.WebApi;
using Microsoft.AspNetCore.Identity;
Expand All @@ -15,15 +16,19 @@ namespace AliasVault.Api.Controllers;
using Identity = AliasVault.Shared.Models.WebApi.Identity;
using Service = AliasVault.Shared.Models.WebApi.Service;

public class AliasController : AuthenticatedRequestController
/// <summary>
/// Alias controller for handling CRUD operations on the database for alias entities.
/// </summary>
/// <param name="context">DbContext instance.</param>
/// <param name="userManager">UserManager instance.</param>
public class AliasController(AliasDbContext context, UserManager<IdentityUser> userManager) : AuthenticatedRequestController(userManager)
{
private readonly AliasDbContext _context;

public AliasController(AliasDbContext context, UserManager<IdentityUser> userManager) : base(userManager)
{
_context = context;
}
private readonly AliasDbContext _context = context;

/// <summary>
/// Get all alias items for the current user.
/// </summary>
/// <returns>List of aliases in JSON format.</returns>
[HttpGet("items")]
public async Task<IActionResult> GetItems()
{
Expand All @@ -42,15 +47,20 @@ public async Task<IActionResult> GetItems()
{
Id = x.Id,
Logo = x.Service.Logo,
Service = x.Service.Name,
CreateDate = x.CreatedAt
Service = x.Service.Name ?? "n/a",
CreateDate = x.CreatedAt,
})

.ToListAsync();

return Ok(aliases);
}

/// <summary>
/// Get a single alias item by its ID.
/// </summary>
/// <param name="aliasId">ID of the alias.</param>
/// <returns>Alias object as JSON.</returns>
[HttpGet("{aliasId}")]
public async Task<IActionResult> GetAlias(Guid aliasId)
{
Expand All @@ -70,11 +80,11 @@ public async Task<IActionResult> GetAlias(Guid aliasId)
{
Service = new Service()
{
Name = x.Service.Name,
Name = x.Service.Name ?? "n/a",
Url = x.Service.Url,
LogoUrl = "",
LogoUrl = string.Empty,
CreatedAt = x.Service.CreatedAt,
UpdatedAt = x.Service.UpdatedAt
UpdatedAt = x.Service.UpdatedAt,
},
Identity = new Identity()
{
Expand All @@ -93,29 +103,28 @@ public async Task<IActionResult> GetAlias(Guid aliasId)
PhoneMobile = x.Identity.PhoneMobile,
BankAccountIBAN = x.Identity.BankAccountIBAN,
CreatedAt = x.Identity.CreatedAt,
UpdatedAt = x.Identity.UpdatedAt
UpdatedAt = x.Identity.UpdatedAt,
},
Password = new AliasVault.Shared.Models.WebApi.Password()
{
Value = x.Passwords.First().Value ?? "",
Description = "",
Value = x.Passwords.First().Value ?? string.Empty,
Description = string.Empty,
CreatedAt = x.Passwords.First().CreatedAt,
UpdatedAt = x.Passwords.First().UpdatedAt
UpdatedAt = x.Passwords.First().UpdatedAt,
},
CreateDate = x.CreatedAt,
LastUpdate = x.UpdatedAt

LastUpdate = x.UpdatedAt,
})
.FirstAsync();

return Ok(aliasObject);
}

/// <summary>
/// Insert a new entry to the database.
/// Insert a new alias to the database.
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
/// <param name="model">Alias model.</param>
/// <returns>ID of newly inserted alias.</returns>
[HttpPut("")]
public async Task<IActionResult> Insert([FromBody] Alias model)
{
Expand All @@ -125,59 +134,61 @@ public async Task<IActionResult> Insert([FromBody] Alias model)
return Unauthorized();
}

var login = new Login();
login.UserId = user.Id;
login.CreatedAt = DateTime.UtcNow;
login.UpdatedAt = DateTime.UtcNow;
login.Identity = new AliasDb.Identity()
var login = new Login
{
NickName = model.Identity.NickName,
FirstName = model.Identity.FirstName,
LastName = model.Identity.LastName,
BirthDate = DateTime.Parse(model.Identity.BirthDate),
Gender = model.Identity.Gender,
AddressStreet = model.Identity.AddressStreet,
AddressCity = model.Identity.AddressCity,
AddressState = model.Identity.AddressState,
AddressZipCode = model.Identity.AddressZipCode,
AddressCountry = model.Identity.AddressCountry,
Hobbies = model.Identity.Hobbies,
EmailPrefix = model.Identity.EmailPrefix,
PhoneMobile = model.Identity.PhoneMobile,
BankAccountIBAN = model.Identity.BankAccountIBAN,
UserId = user.Id,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
UpdatedAt = DateTime.UtcNow,
Identity = new AliasDb.Identity()
{
NickName = model.Identity.NickName,
FirstName = model.Identity.FirstName,
LastName = model.Identity.LastName,
BirthDate = DateTime.Parse(model.Identity.BirthDate ?? "1900-01-01", new CultureInfo("en-US")),
Gender = model.Identity.Gender,
AddressStreet = model.Identity.AddressStreet,
AddressCity = model.Identity.AddressCity,
AddressState = model.Identity.AddressState,
AddressZipCode = model.Identity.AddressZipCode,
AddressCountry = model.Identity.AddressCountry,
Hobbies = model.Identity.Hobbies,
EmailPrefix = model.Identity.EmailPrefix,
PhoneMobile = model.Identity.PhoneMobile,
BankAccountIBAN = model.Identity.BankAccountIBAN,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow,
},
};

login.Passwords.Add(new AliasDb.Password()
{
Value = model.Password.Value,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
UpdatedAt = DateTime.UtcNow,
});

login.Service = new AliasDb.Service()
{
Name = model.Service.Name,
Url = model.Service.Url,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
UpdatedAt = DateTime.UtcNow,
};

_context.Logins.Add(login);
await _context.Logins.AddAsync(login);
await _context.SaveChangesAsync();

return Ok(login.Id);
}

/// <summary>
/// Update an existing entry in the database.
/// Update an existing alias entry in the database.
/// </summary>
/// <param name="model"></param>
/// <param name="aliasId"></param>
/// <returns></returns>
/// <param name="aliasId">The alias ID to update.</param>
/// <param name="model">Alias model.</param>
/// <returns>ID of updated alias entry.</returns>
[HttpPost("{aliasId}")]
public async Task<IActionResult> Update([FromBody] Alias model, Guid aliasId)
public async Task<IActionResult> Update(Guid aliasId, [FromBody] Alias model)
{
var user = await GetCurrentUserAsync();
if (user == null)
Expand All @@ -198,7 +209,7 @@ public async Task<IActionResult> Update([FromBody] Alias model, Guid aliasId)
login.Identity.NickName = model.Identity.NickName;
login.Identity.FirstName = model.Identity.FirstName;
login.Identity.LastName = model.Identity.LastName;
login.Identity.BirthDate = DateTime.Parse(model.Identity.BirthDate);
login.Identity.BirthDate = DateTime.Parse(model.Identity.BirthDate ?? "1900-01-01", new CultureInfo("en-US"));
login.Identity.Gender = model.Identity.Gender;
login.Identity.AddressStreet = model.Identity.AddressStreet;
login.Identity.AddressCity = model.Identity.AddressCity;
Expand All @@ -223,9 +234,10 @@ public async Task<IActionResult> Update([FromBody] Alias model, Guid aliasId)
}

/// <summary>
/// Delete an existing entry from the database.
/// Delete an existing alias entry from the database.
/// </summary>
/// <param name="aliasId"></param>
/// <param name="aliasId">ID of the alias to delete.</param>
/// <returns>HTTP status code.</returns>
[HttpDelete("{aliasId}")]
public async Task<IActionResult> Delete(Guid aliasId)
{
Expand Down
Loading

0 comments on commit d456932

Please sign in to comment.