Summary

Class:ProjectX.PublicApi.Actions.Events.EventsController
Assembly:ProjectX.PublicApi
File(s):C:\private\projects\projectx\backend\src\ProjectX.PublicApi\Actions\Events\EventsController.cs
Covered lines:73
Uncovered lines:12
Coverable lines:85
Total lines:218
Line coverage:85.8%
Branch coverage:62.5%

History

Metrics

MethodCyclomatic complexity  NPath complexity  Sequence coverage  Branch coverage  
.ctor(...)10100100
Get()40100100
Post()7453.8560
Put()30100100
Delete()747580

File(s)

C:\private\projects\projectx\backend\src\ProjectX.PublicApi\Actions\Events\EventsController.cs

#LineLine coverage
 1using System;
 2using System.Threading.Tasks;
 3using Microsoft.AspNetCore.Mvc;
 4using Microsoft.Extensions.Logging;
 5using ProjectX.Entities;
 6using ProjectX.PublicApi.Configuration;
 7using ProjectX.PublicApi.Filters;
 8using ProjectX.Stores;
 9using ServiceBase;
 10
 11namespace ProjectX.PublicApi.Actions.Events
 12{
 13    [ServiceFilter(typeof(AuthContextAttribute))]
 14    [Route("/events")]
 15    public class EventsController : Controller
 16    {
 17        private readonly ApplicationOptions _applicationOptions;
 18        private readonly AppContext _appContext;
 19        private readonly ILogger<EventsController> _logger;
 20        private readonly IEventStore _eventStore;
 21        private readonly IDateTimeAccessor _dateTimeAccessor;
 22
 823        public EventsController(
 824            ApplicationOptions applicationOptions,
 825            AppContext appContext,
 826            ILogger<EventsController> logger,
 827            IEventStore eventStore,
 828            IDateTimeAccessor dateTimeAccessor)
 29        {
 830            this._applicationOptions = applicationOptions;
 831            this._appContext = appContext;
 832            this._logger = logger;
 833            this._eventStore = eventStore;
 834            this._dateTimeAccessor = dateTimeAccessor;
 835        }
 36
 37        [ValidateModel]
 38        [HttpGet]
 39        public async Task<IActionResult> Get(
 40            [FromQuery]GetEventsQuery query)
 41        {
 242            this._logger.LogTrace("Start event get request");
 43
 244            var eventList = await this._eventStore.LoadEventsAsync(
 245                latitude: query.Latitude.Value,
 246                longitude: query.Longitude.Value,
 247                take: query.Take,
 248                skip: query.Skip,
 249                dateFrom: query.DateFrom ?? this._dateTimeAccessor.UtcNow,
 250                userId: this._appContext.User.Id,
 251                perimeter: this._appContext.User.Perimeter,
 252                ageFrom: this._appContext.User.AgeFrom,
 253                ageTo: this._appContext.User.AgeTo
 254            );
 55
 256            var result = eventList.ToModel(this._appContext.User.Id);
 57
 258            this._logger.LogTrace("End event get request");
 59
 260            return new ObjectResult(result);
 261        }
 62
 63        /*
 64         * Not required yet, since the event it self does not have any extra
 65         * Fields more than the event requested via list reqest
 66         *
 67        [HttpGet]
 68        [Route("{eventId}")]
 69        public async Task<IActionResult> Get(Guid eventId)
 70        {
 71            this._logger.LogTrace(
 72                $"Start event get request. By id {eventId}");
 73
 74            var entity = await this._eventStore.LoadEventByIdAsync(eventId);
 75            if (entity == null)
 76            {
 77                this._logger.LogTrace(
 78                    $"End event get request. Event {eventId} not found");
 79
 80                return new NotFoundResult();
 81            }
 82
 83            var result = entity.ToModel(this._appContext.User.Id);
 84
 85            this._logger.LogTrace(
 86                $"End event get request. Found event {eventId}");
 87
 88            return new ObjectResult(result);
 89        }
 90        */
 91
 92        /// <summary>
 93        /// Updates event, only event owner is allowed to update own events
 94        /// </summary>
 95        /// <param name="eventId"></param>
 96        /// <param name="model"></param>
 97        /// <returns></returns>
 98        [HttpPost]
 99        [ValidateModel]
 100        [Route("{eventId}")]
 101        public async Task<IActionResult> Post(Guid eventId,
 102            [FromBody]EventInputModel model)
 103        {
 1104            this._logger.LogTrace(
 1105               $"Start event post request. By id {eventId}");
 106
 107            // Check if event exists
 1108            var entity = await this._eventStore.LoadEventByIdAsync(eventId);
 1109             if (entity == null)
 110            {
 0111                this._logger.LogTrace(
 0112                    $"End event post request. Event {eventId} not found");
 113
 0114                return new NotFoundResult();
 115            }
 116
 117            // Check if context user is event owner
 1118             if (!this._appContext.User.IsOwnerOf(entity))
 119            {
 1120                this._logger.LogTrace(
 1121                   "End event post request. Context user " +
 1122                   $"{this._appContext.User.Id} is not owner of " +
 1123                   $"event {eventId}");
 124
 1125                return new BadRequestResult();
 126            }
 127
 128            // Alrighty then! Update it!
 0129            entity.UpdateFromModel(model);
 0130            await this._eventStore.UpdateEventAsync(entity);
 131
 0132            this._logger.LogTrace(
 0133               $"End event post request. Event updated {eventId}");
 134
 0135            return this.Ok();
 1136        }
 137
 138        /// <summary>
 139        /// Creates event, the authenticated user is automaticly the owner
 140        /// and the first participant of the event.
 141        /// </summary>
 142        /// <param name="model"></param>
 143        /// <returns></returns>
 144        [HttpPut]
 145        [ValidateModel]
 146        public async Task<IActionResult> Put(
 147            [FromBody]EventInputModel model)
 148        {
 1149            this._logger.LogTrace(
 1150               $"Start event put request.");
 151
 1152            var entity = new Event
 1153            {
 1154                Participants = new Participation[]
 1155                {
 1156                    new Participation
 1157                    {
 1158                        Kind = ParticipationKind.Owner,
 1159                        User = this._appContext.User
 1160                    }
 1161                }
 1162            };
 163
 1164            entity.UpdateFromModel(model);
 1165            entity = await this._eventStore.CreateEventAsync(entity);
 166
 1167            this._logger.LogTrace(
 1168               "End event put request. Event created {eventId}");
 169
 1170            return this.CreatedAtAction("Get",
 1171                new { EventId = entity.Id }, null);
 1172        }
 173
 174
 175        /// <summary>
 176        /// Removes the event. Only allowed for event owners
 177        /// </summary>
 178        /// <param name="eventId"></param>
 179        /// <returns></returns>
 180        [HttpDelete]
 181        [ValidateModel]
 182        [Route("{eventId}")]
 183        public async Task<IActionResult> Delete(Guid eventId)
 184        {
 4185            this._logger.LogTrace(
 4186               $"Start event delete request. By id {eventId}");
 187
 188            // Check if event exists
 4189            var entity = await this._eventStore.LoadEventByIdAsync(eventId);
 4190             if (entity == null)
 191            {
 2192                this._logger.LogTrace(
 2193                    $"End event delete request. Event {eventId} not found");
 194
 2195                return new NotFoundResult();
 196            }
 197
 198            // Check if context user is event owner
 2199             if (!this._appContext.User.IsOwnerOf(entity))
 200            {
 2201                this._logger.LogTrace(
 2202                   "End event delete request. Context user " +
 2203                   $"{this._appContext.User.Id} is not owner of " +
 2204                   $"event {eventId}");
 205
 2206                return new BadRequestResult();
 207            }
 208
 209            // Alrighty then! Remove it!
 0210            await this._eventStore.DeleteEventAsync(entity);
 211
 0212            this._logger.LogTrace(
 0213               $"End event delete request. Event deleted {eventId}");
 214
 0215            return this.Ok();
 4216        }
 217    }
 218}