Summary

Class:ProjectX.PublicApi.Actions.Events.EventModelExtensions
Assembly:ProjectX.PublicApi
File(s):C:\private\projects\projectx\backend\src\ProjectX.PublicApi\Actions\Events\EventModelExtensions.cs
Covered lines:41
Uncovered lines:3
Coverable lines:44
Total lines:93
Line coverage:93.1%
Branch coverage:50%

History

Metrics

MethodCyclomatic complexity  NPath complexity  Sequence coverage  Branch coverage  
ToModel(...)10100100
UpdateFromModel(...)10100100
ToModel(...)4466.6760
ToModel(...)10100100
ToModel(...)5410060

File(s)

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

#LineLine coverage
 1using System;
 2using System.Linq;
 3using ProjectX.Entities;
 4using ProjectX.Models;
 5
 6namespace ProjectX.PublicApi.Actions.Events
 7{
 8    public static class EventModelExtensions
 9    {
 10        public static EventModel ToModel(this Event entity)
 11        {
 312            return new EventModel
 313            {
 314                Id = entity.Id,
 315                CategoryId = entity.CategoryId,
 316                Title = entity.Title,
 317                Info = entity.Info,
 318                DateFrom = entity.DateFrom,
 319                DateTo = entity.DateTo
 320            };
 21        }
 22
 23        /// <summary>
 24        /// Updates a entity from DTO
 25        /// </summary>
 26        /// <param name="entity"></param>
 27        /// <returns></returns>
 28        public static Event UpdateFromModel(
 29            this Event entity,
 30            EventInputModel model)
 31        {
 132            entity.CategoryId = model.CategoryId;
 133            entity.DateFrom = model.DateFrom;
 134            entity.DateTo = model.DateTo;
 135            entity.Info = model.Info;
 136            entity.Title = model.Title;
 37
 138            return entity;
 39        }
 40
 41        public static EventModel ToModel(this Event entity, Guid userId)
 42        {
 343            var model = entity.ToModel();
 44
 345             if (entity.CreatedById.HasValue &&
 346                userId.Equals(entity.CreatedById))
 47            {
 48                // Return all the data, also not confirmed particiapts
 049                model.Participants = entity.Participants
 050                    .Select(EventModelExtensions.ToModel).ToArray();
 051            }
 52            else
 53            {
 54                // Only show the owner and accepted participants
 355                model.Participants = entity.Participants.Where(x =>
 1056                    x.Kind == Entities.ParticipationKind.Accepted ||
 1057                    x.Kind == Entities.ParticipationKind.Owner)
 358                    .Select(EventModelExtensions.ToModel).ToArray();
 59            }
 60
 361            return model;
 62        }
 63
 64        public static PagedList<EventModel> ToModel(
 65            this PagedList<Event> entity, Guid userId)
 66        {
 267            return new PagedList<EventModel>
 268            {
 269                Total = entity.Total,
 270                Skip = entity.Skip,
 271                Take = entity.Take,
 572                Items = entity.Items.Select(s => s.ToModel(userId)).ToArray()
 273            };
 74        }
 75
 76        public static ParticipantModel ToModel(
 77             this Participation entity)
 78        {
 579             var image = entity.User.Images?.FirstOrDefault();
 80
 581             return new ParticipantModel
 582            {
 583                ImageId = image != null ? image.ImageId : Guid.Empty,
 584                UserId = entity.User.Id,
 585                Kind = entity.Kind,
 586                Name = entity.User.Name,
 587                Age = entity.User.Age,
 588                Gender = entity.User.Gender,
 589                Info = entity.User.Info
 590            };
 91        }
 92    }
 93}