Summary

Class:ProjectX.PublicApi.Actions.Users.UsersController
Assembly:ProjectX.PublicApi
File(s):C:\private\projects\projectx\backend\src\ProjectX.PublicApi\Actions\Users\UsersController.cs
Covered lines:36
Uncovered lines:0
Coverable lines:36
Total lines:104
Line coverage:100%
Branch coverage:100%

History

Metrics

MethodCyclomatic complexity  NPath complexity  Sequence coverage  Branch coverage  
.ctor(...)10100100
Get()54100100
Post()42100100

File(s)

C:\private\projects\projectx\backend\src\ProjectX.PublicApi\Actions\Users\UsersController.cs

#LineLine coverage
 1using System;
 2using System.Threading.Tasks;
 3using Microsoft.AspNetCore.Mvc;
 4using Microsoft.Extensions.Logging;
 5using ProjectX.Entities;
 6using ProjectX.PublicApi.Filters;
 7using ProjectX.Stores;
 8
 9namespace ProjectX.PublicApi.Actions.Users
 10{
 11    [ServiceFilter(typeof(AuthContextAttribute))]
 12    [Route("/users")]
 13    public class UsersController : Controller
 14    {
 15        private readonly AppContext _appContext;
 16        private readonly ILogger<UsersController> _logger;
 17        private readonly IUserStore _userStore;
 18
 719        public UsersController(
 720            AppContext appContext,
 721            ILogger<UsersController> logger,
 722            IUserStore userStore)
 23        {
 724            this._appContext = appContext;
 725            this._logger = logger;
 726            this._userStore = userStore;
 727        }
 28
 29        /// <summary>
 30        /// Get user entity
 31        /// </summary>
 32        /// <param name="userId"><see cref="User"/> primary key</param>
 33        /// <returns></returns>
 34        [HttpGet]
 35        [Route("{userId}")]
 36        public async Task<IActionResult> Get(Guid userId)
 37        {
 538            this._logger.LogTrace("Start user get request");
 39
 40            // return UserModel if accessed as resource owner
 541             if (this._appContext.User.Id.Equals(userId))
 42            {
 343                var result = this._appContext.User.ToModel();
 44
 345                this._logger.LogTrace(
 346                    "End user get request. Authenticated user found: " +
 347                    $"{this._appContext.User.Id}");
 48
 349                return new ObjectResult(result);
 50            }
 51
 52            // return UserDetailsModel if accesses not owned resource
 53            else
 54            {
 255                var user = await this._userStore.LoadUserByIdAsync(userId);
 256                 if (user == null)
 57                {
 158                    this._logger.LogTrace(
 159                        $"End user get request. User not found { userId}");
 60
 161                    return new NotFoundResult();
 62                }
 63
 164                var result = user.ToDetailsModel();
 65
 166                this._logger.LogTrace(
 167                    $"End user get request. User found: {result.Id}");
 68
 169                return new ObjectResult(result);
 70            }
 571        }
 72
 73        /// <summary>
 74        /// Update user entity
 75        /// </summary>
 76        /// <param name="userId"></param>
 77        /// <param name="model"></param>
 78        /// <returns></returns>
 79        [HttpPost]
 80        [Route("{userId}")]
 81        [ValidateModel]
 82        public async Task<IActionResult> Post(Guid userId,
 83            [FromBody]UserModel model)
 84        {
 285            this._logger.LogTrace("Start user update request");
 86
 287             if (!userId.Equals(this._appContext.User.Id))
 88            {
 189                this._logger.LogTrace("End user update request: Not allowed " +
 190                    "to update other users");
 91
 192                return new StatusCodeResult(403);
 93            }
 94
 195            var user = await this._userStore.UpdateUserAsync(
 196                this._appContext.User.UpdateFromModel(model));
 97
 198            this._logger.LogTrace(
 199                $"End user update request: User updated {userId}");
 100
 1101            return this.Ok();
 2102        }
 103    }
 104}