public async Task ShouldCreateMainEvent()
        {
            string   name          = "Test Event 3";
            DateTime startDateTime = DateTime.Now;
            DateTime endDateTime   = DateTime.Now;

            MainEventService    mainEventService    = new MainEventService(_dbContext, _userManager);
            SecurityService     securityService     = new SecurityService(_dbContext, _userManager, _roleManager);
            MainEventController mainEventController = new MainEventController(mainEventService, securityService);

            CreateUser();
            SetUser(mainEventController, _createdUser.Entity.Id);
            CreateOrganizers();
            CreateVenues();

            MainEventVm mainEventVm = new MainEventVm {
                Name = name, StartDateTime = startDateTime, EndDateTime = endDateTime, VenueId = 2, OrganizerId = 2
            };

            await mainEventController.CreateMainEventAsync(mainEventVm);

            ActionResult <List <MainEventListVm> > result = await mainEventController.GetMainEventsAsync();

            List <MainEventListVm> returnedMainEvents = (List <MainEventListVm>)((OkObjectResult)result.Result).Value;

            Assert.AreEqual(2, returnedMainEvents.Count);
            Assert.AreEqual(mainEventVm.Name, returnedMainEvents[1].Name);
            Assert.AreEqual(mainEventVm.StartDateTime, returnedMainEvents[1].StartDateTime);
            Assert.AreEqual(mainEventVm.EndDateTime, returnedMainEvents[1].EndDateTime);
        }
        public async Task ShouldUpdateMainEvent()
        {
            MainEventService    mainEventService    = new MainEventService(_dbContext, _userManager);
            SecurityService     securityService     = new SecurityService(_dbContext, _userManager, _roleManager);
            MainEventController mainEventController = new MainEventController(mainEventService, securityService);

            CreateUser();
            SetUser(mainEventController, _createdUser.Entity.Id);
            CreateMainEvents();

            string   newName      = "Test name 3";
            DateTime newStartTime = DateTime.Now.AddDays(4);
            DateTime newEndTime   = DateTime.Now.AddDays(8);

            MainEventVm mainEventVm = new MainEventVm {
                Id = 2, Name = newName, StartDateTime = newStartTime, EndDateTime = newEndTime, VenueId = 2, OrganizerId = 2
            };

            await mainEventController.UpdateMainEventAsync(mainEventVm);

            // Check that only one has been changed
            MainEvent mainEvent1 = _dbContext.MainEvents.Find(2);

            Assert.AreEqual(newName, mainEvent1.Name);
            Assert.AreEqual(newStartTime, mainEvent1.StartDateTime);
            Assert.AreEqual(newEndTime, mainEvent1.EndDateTime);
        }
示例#3
0
        /// <summary>
        /// Updates a specific Event.
        /// </summary>
        /// <param name="mainEventVm"></param>
        public async Task UpdateMainEventAsync(MainEventVm maineventVm)
        {
            var existingMainEvent = await _dbContext.MainEvents
                                    .Where(a => a.Id != maineventVm.Id && a.Name == maineventVm.Name)
                                    .SingleOrDefaultAsync();


            if (existingMainEvent != null)
            {
                throw new HttpException(HttpStatusCode.Conflict, $"Arrangementet: {existingMainEvent.Name} eksisterer allerede");
            }

            existingMainEvent = await _dbContext.MainEvents.FindAsync(maineventVm.Id);

            if (existingMainEvent == null)
            {
                throw new HttpException(HttpStatusCode.NotFound, "Fant ikke arrangementet");
            }

            existingMainEvent.Id               = maineventVm.Id;
            existingMainEvent.Name             = maineventVm.Name;
            existingMainEvent.StartDateTime    = maineventVm.StartDateTime;
            existingMainEvent.EndDateTime      = maineventVm.EndDateTime;
            existingMainEvent.VenueId          = maineventVm.VenueId;
            existingMainEvent.InfoComments     = maineventVm.InfoComments;
            existingMainEvent.OrganizerWebPage = maineventVm.OrganizerWebPage;

            _dbContext.Update(existingMainEvent);
            await _dbContext.SaveChangesAsync();
        }
        public async Task <ActionResult> UpdateMainEventAsync(MainEventVm mainEventVm)
        {
            var organizers = await _securityService.GetOrganizersUserIsAdminAtAsync(User.FindFirstValue(ClaimTypes.NameIdentifier));

            if (!organizers.Any(a => a.Id == mainEventVm.OrganizerId))
            {
                return(Forbid());
            }

            await _mainEventService.UpdateMainEventAsync(mainEventVm);

            Log.Information("MainEvent {@mainEventVm} updated", mainEventVm);
            return(Ok());
        }
        public async Task <ActionResult> CreateMainEventAsync(MainEventVm mainEventVm)
        {
            var userId     = User.FindFirstValue(ClaimTypes.NameIdentifier);
            var organizers = await _securityService.GetOrganizersUserIsAdminAtAsync(userId); //Sjekker hvis bruker er orgadmin i org de prøver å opprette arrangement i

            if (!organizers.Any(a => a.Id == mainEventVm.OrganizerId))
            {
                return(Forbid());
            }

            await _mainEventService.CreateMainEventAsync(mainEventVm, userId);

            Log.Information("MainEvent {@mainEventVm} created by User {userId} saved to db", mainEventVm, userId);
            return(Ok());
        }
        public async Task ShouldGetMainEventById()
        {
            CreateUser();
            CreateMainEvents();

            // Check that we can get both by id
            MainEventService    mainEventService    = new MainEventService(_dbContext, _userManager);
            SecurityService     securityService     = new SecurityService(_dbContext, _userManager, _roleManager);
            MainEventController mainEventController = new MainEventController(mainEventService, securityService);

            ActionResult <MainEventVm> result1 = await mainEventController.GetMainEventAsync(1);

            MainEventVm returnedMainEvent = (MainEventVm)((OkObjectResult)result1.Result).Value;

            Assert.AreEqual(1, returnedMainEvent.Id);
            Assert.AreEqual("Event 1", returnedMainEvent.Name);

            ActionResult <MainEventVm> result2 = await mainEventController.GetMainEventAsync(2);

            MainEventVm returnedMainEvent2 = (MainEventVm)((OkObjectResult)result2.Result).Value;

            Assert.AreEqual(2, returnedMainEvent2.Id);
            Assert.AreEqual(_eventName2, returnedMainEvent2.Name);
        }
示例#7
0
        /// <summary>
        /// Creates a new Event.
        /// </summary>
        /// <param name="mainEventVm"></param>
        /// /// <param name="userId"></param>
        /// <remarks>
        /// A default management crew with crew permissions is created along with the MainEvent.
        /// </remarks>
        public async Task CreateMainEventAsync(MainEventVm maineventVm, string userId)
        {
            var existingMainEvent = await _dbContext.MainEvents
                                    .Where(a => a.Id == maineventVm.Id || a.Name == maineventVm.Name)
                                    .SingleOrDefaultAsync();


            if (existingMainEvent != null)
            {
                throw new HttpException(HttpStatusCode.Conflict, $"Arrangementet: {existingMainEvent.Name} eksisterer allerede");
            }

            var leaderCrew = new Crew
            {
                Name  = "Ledelse",
                Users = new List <CrewUser>
                {
                    new CrewUser
                    {
                        ApplicationUserId = userId,
                        IsLeader          = true,
                        Comment           = "Arrangementsleder"
                    }
                },
                CrewPermissions = new List <CrewPermission>()
            };

            var crewPermissionTypes = Enum.GetValues(typeof(CrewPermissionType)).Cast <CrewPermissionType>();

            foreach (var crewPermissionType in crewPermissionTypes)
            {
                leaderCrew.CrewPermissions
                .Add(new CrewPermission
                {
                    PermissionType = crewPermissionType
                });
            }

            var mainevent = new MainEvent
            {
                Name             = maineventVm.Name,
                StartDateTime    = maineventVm.StartDateTime,
                EndDateTime      = maineventVm.EndDateTime,
                OrganizerId      = maineventVm.OrganizerId,
                VenueId          = maineventVm.VenueId,
                InfoComments     = maineventVm.InfoComments,
                OrganizerWebPage = maineventVm.OrganizerWebPage,
                Crews            = new List <Crew>
                {
                    leaderCrew
                }
            };

            _dbContext.MainEvents.Add(mainevent);
            await _dbContext.SaveChangesAsync();

            var user = await _dbContext.ApplicationUsers
                       .Where(a => a.Id == userId)
                       .SingleOrDefaultAsync();

            user.CurrentMainEventId = mainevent.Id;
            await _dbContext.SaveChangesAsync();
        }