Skip to content
This repository has been archived by the owner on May 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #178 from Laixer/feature/cleanup
Browse files Browse the repository at this point in the history
Feature/cleanup - Increase code quality
  • Loading branch information
tabeckers authored Nov 16, 2020
2 parents 457bb87 + de6e474 commit 0da26a3
Show file tree
Hide file tree
Showing 125 changed files with 757 additions and 3,443 deletions.
20 changes: 0 additions & 20 deletions Laixer.Infra.Npgsql/IDatabaseProvider.cs

This file was deleted.

24 changes: 0 additions & 24 deletions Laixer.Infra.Npgsql/Laixer.Infra.Npgsql.csproj

This file was deleted.

14 changes: 0 additions & 14 deletions Laixer.Utility/Laixer.Utility.csproj

This file was deleted.

3 changes: 1 addition & 2 deletions Swabbr.Api/Authentication/SwabbrIdentityUser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Laixer.Utility.Extensions;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity;
using Swabbr.Core.Entities;
using Swabbr.Core.Enums;
using System;
Expand Down
2 changes: 1 addition & 1 deletion Swabbr.Api/Authentication/UserStore.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Laixer.Utility.Extensions;
using Swabbr.Core.Extensions;
using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
Expand Down
4 changes: 2 additions & 2 deletions Swabbr.Api/Configuration/JwtConfigurationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Laixer.Utility.Exceptions;
using Laixer.Utility.Extensions;
using Swabbr.Core.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Swabbr.Core.Extensions;

namespace Swabbr.Api.Configuration
{
Expand Down
86 changes: 46 additions & 40 deletions Swabbr.Api/Controllers/AuthenticationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ public AuthenticationController(IUserService userService,
logger = (loggerFactory != null) ? loggerFactory.CreateLogger(nameof(AuthenticationController)) : throw new ArgumentNullException(nameof(loggerFactory));
}

// TODO This does too much, maybe let the UserService handle a bunch.
/// <summary>
/// Create a new user account.
/// Create a new user account.
/// </summary>
/// <param name="input"><see cref="UserRegisterInputModel"/></param>
/// <returns><see cref="OkResult"/> or <see cref="BadRequestResult"/></returns>
Expand All @@ -72,50 +73,55 @@ public async Task<IActionResult> RegisterAsync([FromBody] UserRegisterInputModel

try
{
using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
// Make this operation transactional
using var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);

// Edge cases
if ((await _userManager.FindByEmailAsync(input.Email).ConfigureAwait(false)) != null)
{
// Edge cases
if ((await _userManager.FindByEmailAsync(input.Email).ConfigureAwait(false)) != null)
{
return BadRequest(this.Error(ErrorCodes.EntityAlreadyExists, "User email already registered"));
}
return BadRequest(this.Error(ErrorCodes.EntityAlreadyExists, "User email already registered"));
}
if (await _userService.ExistsNicknameAsync(input.Nickname).ConfigureAwait(false))
{
return BadRequest(this.Error(ErrorCodes.EntityAlreadyExists, "Nickname already exists"));
}

if (await _userService.ExistsNicknameAsync(input.Nickname).ConfigureAwait(false))
{
return BadRequest(this.Error(ErrorCodes.EntityAlreadyExists, "Nickname already exists"));
}
// Construct a new identity user for a new user based on the given input.
// The entity will also be created in our own data store.
var identityUser = new SwabbrIdentityUser
{
Email = input.Email,
Nickname = input.Nickname
};

// Construct a new identity user for a new user based on the given input
var identityUser = new SwabbrIdentityUser
{
Email = input.Email,
Nickname = input.Nickname
};
// This call assigns the id to the identityUser object.
var identityResult = await _userManager.CreateAsync(identityUser, input.Password).ConfigureAwait(false);
if (!identityResult.Succeeded)
{
return BadRequest(this.Error(ErrorCodes.InvalidOperation, "Could not create new user, contact your administrator"));
}

var user = await _userManager.CreateAsync(identityUser, input.Password).ConfigureAwait(false);
if (!user.Succeeded) { return BadRequest(this.Error(ErrorCodes.InvalidOperation, "Could not create new user, contact your administrator")); }
// Update all other properties.
// The nickname is handled by the creation call.
var user = await _userService.GetAsync(identityUser.Id).ConfigureAwait(false);

user.BirthDate = input.BirthDate;
user.Country = input.Country;
user.FirstName = input.FirstName;
user.Gender = MapperEnum.Map(input.Gender);
user.IsPrivate = input.IsPrivate;
user.LastName = input.LastName;
user.ProfileImageBase64Encoded = input.ProfileImageBase64Encoded;

// Update all other properties (if present)
var updatedUser = await _userService.UpdateAsync(new UserUpdateWrapper
{
UserId = identityUser.Id,
BirthDate = input.BirthDate,
Country = input.Country,
FirstName = input.FirstName,
Gender = MapperEnum.Map(input.Gender),
IsPrivate = input.IsPrivate,
LastName = input.LastName,
//Nickname = input.Nickname, Done in creation call
ProfileImageBase64Encoded = input.ProfileImageBase64Encoded
}).ConfigureAwait(false);
var updatedUser = await _userService.UpdateAsync(user).ConfigureAwait(false);

// Map and return
scope.Complete();
return Ok(new UserAuthenticationOutputModel
{
User = MapperUser.Map(updatedUser)
});
}
scope.Complete();

// Map.
var result = MapperUser.Map(updatedUser);

// Return.
return Ok(result);
}
catch (InvalidProfileImageStringException)
{
Expand Down Expand Up @@ -173,7 +179,7 @@ public async Task<IActionResult> LoginAsync([FromBody] UserLoginInputModel input
Claims = await _userManager.GetClaimsAsync(identityUser).ConfigureAwait(false),
Roles = await _userManager.GetRolesAsync(identityUser).ConfigureAwait(false),
User = MapperUser.Map(await _userService.GetAsync(identityUser.Id).ConfigureAwait(false)),
UserSettings = MapperUser.Map(await _userService.GetUserSettingsAsync(identityUser.Id).ConfigureAwait(false))
UserSettings = MapperUser.MapToSettings(await _userService.GetAsync(identityUser.Id).ConfigureAwait(false))
});
}

Expand Down
3 changes: 1 addition & 2 deletions Swabbr.Api/Controllers/DebugController.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using Laixer.Utility.Extensions;
using Swabbr.Core.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.WebUtilities;
//using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.Azure.Storage.Blob;
using Microsoft.Net.Http.Headers;
using Swabbr.Api.Authentication;
Expand Down
7 changes: 4 additions & 3 deletions Swabbr.Api/Controllers/FollowRequestsController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Laixer.Utility.Extensions;
using Swabbr.Core.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -13,6 +13,7 @@
using Swabbr.Core.Entities;
using Swabbr.Core.Enums;
using Swabbr.Core.Exceptions;
using Swabbr.Core.Extensions;
using Swabbr.Core.Interfaces.Repositories;
using Swabbr.Core.Interfaces.Services;
using Swabbr.Core.Types;
Expand Down Expand Up @@ -182,8 +183,8 @@ public async Task<IActionResult> SendAsync(Guid receiverId)
// Try to perform the request
try
{
var followRequest = await _followRequestService.SendAsync(requesterId, receiverId).ConfigureAwait(false);
return Ok(MapperFollowRequest.Map(followRequest));
var followRequestId = await _followRequestService.SendAsync(requesterId, receiverId).ConfigureAwait(false);
return Ok(MapperFollowRequest.Map(await _followRequestService.GetAsync(followRequestId)));
}
catch (EntityAlreadyExistsException e)
{
Expand Down
2 changes: 1 addition & 1 deletion Swabbr.Api/Controllers/LivestreamsController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Laixer.Utility.Extensions;
using Swabbr.Core.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
Expand Down
2 changes: 1 addition & 1 deletion Swabbr.Api/Controllers/ReactionsController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Laixer.Utility.Extensions;
using Swabbr.Core.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
Expand Down
2 changes: 1 addition & 1 deletion Swabbr.Api/Controllers/TestingEndpointsController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Laixer.Utility.Extensions;
using Swabbr.Core.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
Expand Down
2 changes: 1 addition & 1 deletion Swabbr.Api/Controllers/TypesController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Laixer.Utility.Extensions;
using Swabbr.Core.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
Expand Down
43 changes: 22 additions & 21 deletions Swabbr.Api/Controllers/UserSettingsController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Laixer.Utility.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
Expand All @@ -9,6 +8,8 @@
using Swabbr.Api.Mapping;
using Swabbr.Api.ViewModels;
using Swabbr.Core.Entities;
using Swabbr.Core.Extensions;
using Swabbr.Core.Extensions;
using Swabbr.Core.Interfaces.Services;
using System;
using System.Net;
Expand Down Expand Up @@ -53,17 +54,15 @@ public async Task<IActionResult> GetAsync()
{
try
{
// Act.
var identityUser = await _userManager.GetUserAsync(User).ConfigureAwait(false);
if (identityUser == null) { throw new InvalidOperationException("Can't get user settings when no user is logged in"); }
var user = await _userService.GetAsync(identityUser.Id).ConfigureAwait(false);

var result = await _userService.GetUserSettingsAsync(identityUser.Id).ConfigureAwait(false);
return Ok(new UserSettingsOutputModel
{
DailyVlogRequestLimit = result.DailyVlogRequestLimit,
FollowMode = result.FollowMode.GetEnumMemberAttribute(),
IsPrivate = result.IsPrivate,
UserId = result.UserId
});
// Map.
var result = MapperUser.MapToSettings(user);

// Return.
return Ok(result);
}
catch (Exception e)
{
Expand All @@ -86,19 +85,21 @@ public async Task<IActionResult> UpdateAsync([FromBody] UserSettingsInputModel i
if (input == null) { throw new ArgumentNullException("Model can't be null"); }
if (!ModelState.IsValid) { throw new ArgumentException("Model isn't valid"); }

// Act.
var identityUser = await _userManager.GetUserAsync(User).ConfigureAwait(false);
if (identityUser == null) { throw new InvalidOperationException("Can't update user settings when no user is logged in"); }
var user = await _userService.GetAsync(identityUser.Id).ConfigureAwait(false);

user.DailyVlogRequestLimit = input.DailyVlogRequestLimit;
user.FollowMode = MapperEnum.Map(input.FollowMode);
user.IsPrivate = input.IsPrivate;

await _userService.UpdateAsync(user).ConfigureAwait(false);

var settings = new UserSettings
{
DailyVlogRequestLimit = (int)input.DailyVlogRequestLimit,
FollowMode = MapperEnum.Map(input.FollowMode),
UserId = identityUser.Id,
IsPrivate = input.IsPrivate
};
await _userService.UpdateSettingsAsync(settings).ConfigureAwait(false);
// Map.
var result = MapperUser.MapToSettings(await _userService.GetAsync(identityUser.Id).ConfigureAwait(false));

return Ok(MapperUser.Map(await _userService.GetUserSettingsAsync(identityUser.Id).ConfigureAwait(false)));
// Return.
return Ok(result);
}
catch (Exception e)
{
Expand Down
18 changes: 6 additions & 12 deletions Swabbr.Api/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Laixer.Utility.Extensions;
using Swabbr.Core.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -24,18 +24,16 @@

namespace Swabbr.Api.Controllers
{

/// <summary>
/// Controller for handling requests related to users. This is NOT used for
/// processing any requests regarding the identity side of the users.
/// Controller for handling requests related to users. This is NOT used for
/// processing any requests regarding the identity side of the users.
/// </summary>
[Authorize]
[ApiController]
[ApiVersion("1")]
[Route("api/{version:apiVersion}/users")]
public class UsersController : ControllerBase
{

private readonly IUserWithStatsRepository _userWithStatsRepository;
private readonly IUserService _userService;
private readonly UserManager<SwabbrIdentityUser> _userManager;
Expand Down Expand Up @@ -148,9 +146,9 @@ public async Task<IActionResult> UpdateAsync([FromBody] UserUpdateInputModel inp
if (!ModelState.IsValid) { return BadRequest(this.Error(ErrorCodes.InvalidInput, "Post body is invalid")); }

var identityUser = await _userManager.GetUserAsync(User).ConfigureAwait(false);
var updatedUser = await _userService.UpdateAsync(new UserUpdateWrapper
var updatedUser = await _userService.UpdateAsync(new SwabbrUser
{
UserId = identityUser.Id,
Id = identityUser.Id,
BirthDate = input.BirthDate,
Country = input.Country,
FirstName = input.FirstName,
Expand Down Expand Up @@ -240,9 +238,7 @@ public async Task<IActionResult> GetStatisticsAsync([FromRoute] Guid userId)
{
if (userId.IsNullOrEmpty()) { return BadRequest(this.Error(ErrorCodes.InvalidInput, "User id can't be null or empty")); }

return Ok(MapperUser.Map(
await _userService.GetUserStatisticsAsync(userId)
.ConfigureAwait(false)));
return Ok(MapperUser.MapToStatistics(await _userService.GetWithStatisticsAsync(userId).ConfigureAwait(false)));
}
catch (Exception e)
{
Expand Down Expand Up @@ -271,7 +267,5 @@ public async Task<IActionResult> GetStatisticsSelfAsync()
return Conflict(this.Error(ErrorCodes.InvalidOperation, "Could not get statistics for self"));
}
}

}

}
Loading

0 comments on commit 0da26a3

Please sign in to comment.