Summary

Class:ProjectX.PublicApi.Actions.Participants.ParticipantsController
Assembly:ProjectX.PublicApi
File(s):C:\private\projects\projectx\backend\src\ProjectX.PublicApi\Actions\Participants\ParticipantsController.cs
Covered lines:0
Uncovered lines:65
Coverable lines:65
Total lines:169
Line coverage:0%
Branch coverage:0%

History

Metrics

MethodCyclomatic complexity  NPath complexity  Sequence coverage  Branch coverage  
.ctor(...)1000
Post()7400
Put()7400
Delete()1000

File(s)

C:\private\projects\projectx\backend\src\ProjectX.PublicApi\Actions\Participants\ParticipantsController.cs

#LineLine coverage
 1using System.Linq;
 2using System;
 3using System.Threading.Tasks;
 4using Microsoft.AspNetCore.Mvc;
 5using Microsoft.Extensions.Logging;
 6using ProjectX.Entities;
 7using ProjectX.PublicApi.Configuration;
 8using ProjectX.PublicApi.Filters;
 9using ProjectX.Stores;
 10
 11namespace ProjectX.PublicApi.Actions.Participants
 12{
 13    [ServiceFilter(typeof(AuthContextAttribute))]
 14    [Route("/events/{eventId}/participants")]
 15    public class ParticipantsController : Controller
 16    {
 17        private readonly ApplicationOptions _applicationOptions;
 18        private readonly AppContext _appContext;
 19        private readonly ILogger<ParticipantsController> _logger;
 20        private readonly IParticipationStore _participationStore;
 21        private readonly IEventStore _eventStore;
 22
 023        public ParticipantsController(
 024            ApplicationOptions applicationOptions,
 025            AppContext appContext,
 026            ILogger<ParticipantsController> logger,
 027            IParticipationStore participationStore,
 028            IEventStore eventStore)
 29        {
 030            this._applicationOptions = applicationOptions;
 031            this._appContext = appContext;
 032            this._logger = logger;
 033            this._participationStore = participationStore;
 034            this._eventStore = eventStore;
 035        }
 36
 37        /// <summary>
 38        /// Changes the status of participant, only the event owner is
 39        /// allowed to change particiapts state
 40        /// </summary>
 41        /// <param name="eventId"></param>
 42        /// <param name="userId"></param>
 43        /// <returns></returns>
 44        [HttpPost]
 45        [Route("{userId}")]
 46        [ValidateModel]
 47        public async Task<IActionResult> Post(
 48            Guid eventId, Guid userId,
 49            [FromBody] ParticipationInputModel model)
 50        {
 051            this._logger.LogTrace(
 052                $"Start participant post request. " +
 053                $"By eventId {eventId}, userId {userId}");
 54
 55            // Load parcitipation entity
 056            var entity = await this._participationStore
 057                .LoadParticipationAsync(eventId, userId);
 58
 059             if (entity == null)
 60            {
 061                this._logger.LogTrace($"End participant post request. " +
 062                  $"Participation {eventId}, {userId} not found");
 63
 064                return new NotFoundResult();
 65            }
 66
 067             if (!this._appContext.User.IsOwnerOf(entity))
 68            {
 069                this._logger.LogTrace(
 070                   "End participant post request. Context user " +
 071                   $"{this._appContext.User.Id} is not owner of " +
 072                   $"participation {eventId}, {userId}");
 73
 074                return new BadRequestResult();
 75            }
 76
 77            // Update parcitipation entity
 078            entity.Kind = model.Kind;
 79
 080            await this._participationStore
 081                .UpdateParticipationAsync(entity);
 82
 083            this._logger.LogTrace(
 084                $"End participant post request. " +
 085                $"By eventId {eventId}, userId {userId}");
 86
 087            return this.Ok();
 88        }
 89
 90        /// <summary>
 91        /// Adds a new participant with requested state to a event.
 92        /// Only event owner may add participants already with accepted state
 93        /// Otherwise only requested state is allowed.
 94        /// Participant owner is not allowed to request participation for
 95        /// own event
 96        /// </summary>
 97        /// <param name="eventId"></param>
 98        /// <returns></returns>
 99        [HttpPut]
 100        public async Task<IActionResult> Put(Guid eventId)
 101        {
 0102            this._logger.LogTrace(
 0103                $"Start participant put request. By eventId {eventId}");
 104
 105            // Check if event exists
 0106            var evnt = await this._eventStore.LoadEventByIdAsync(eventId);
 0107             if (evnt == null)
 108            {
 0109                this._logger.LogTrace($"End participant put request. " +
 0110                  $"Event {eventId} not found");
 111
 0112                return new NotFoundResult();
 113            }
 114
 115            // Check if user already a event participant
 0116             if (evnt.Participants.Any(c =>
 0117                c.UserId == this._appContext.User.Id))
 118            {
 0119                this._logger.LogTrace($"End participant put request. " +
 0120                  $"Event {eventId} already contains a user " +
 0121                  $"{this._appContext.User.Id} as participant.");
 122
 0123                return new BadRequestResult();
 124            }
 125
 0126            var participation = new Participation
 0127            {
 0128                EventId = evnt.Id,
 0129                UserId = this._appContext.User.Id,
 0130                Kind = ParticipationKind.Requested
 0131            };
 132
 0133            await this._participationStore
 0134                .CreateParticipationAsync(participation);
 135
 0136            this._logger.LogTrace(
 0137                $"End participant put request. By eventId {eventId}");
 138
 0139            throw new NotImplementedException();
 140        }
 141
 142        /// <summary>
 143        /// Removes the participant from event. Event owner may remove every
 144        /// participant except him self. Not event owner may remove only
 145        /// his own participation.
 146        /// </summary>
 147        /// <param name="eventId"></param>
 148        /// <param name="userId"></param>
 149        /// <returns></returns>
 150        [HttpDelete]
 151        [Route("{userId}")]
 152        public async Task<IActionResult> Delete(
 153            Guid eventId, Guid userId)
 154        {
 0155            this._logger.LogTrace(
 0156                $"Start participant delete request. " +
 0157                $"By eventId { eventId}, userId { userId}");
 158
 159
 160
 161
 0162            this._logger.LogTrace(
 0163                $"End participant delete request. " +
 0164                $"By eventId { eventId}, userId { userId}");
 165
 0166            throw new NotImplementedException();
 167        }
 168    }
 169}