Example #1
0
        public async Task <ViewModels.Session> UpdateAsync(Guid id, ViewModels.Session session, CancellationToken ct)
        {
            if (!(await _authorizationService.AuthorizeAsync(_user, null, new ContentDeveloperRequirement())).Succeeded)
            {
                throw new ForbiddenException();
            }

            var sessionToUpdate = await _context.Sessions.SingleOrDefaultAsync(v => v.Id == id, ct);

            if (sessionToUpdate == null)
            {
                throw new EntityNotFoundException <Session>();
            }

            session.DateCreated  = sessionToUpdate.DateCreated;
            session.CreatedBy    = sessionToUpdate.CreatedBy;
            session.DateModified = DateTime.UtcNow;
            session.ModifiedBy   = _user.GetId();
            Mapper.Map(session, sessionToUpdate);

            _context.Sessions.Update(sessionToUpdate);
            await _context.SaveChangesAsync(ct);

            return(_mapper.Map(sessionToUpdate, session));
        }
Example #2
0
        public ViewModels.Session GetCurrentSession(string dictaatName, string userId = null)
        {
            var count = this._context.DictaatSession
                        .Include(ds => ds.DictaatDetails)
                        .Where(s => s.DictaatDetailsId == dictaatName).Count();

            if (count == 0)
            {
                var s = new DictaatSession()
                {
                    DictaatDetailsId = dictaatName,
                    StartedOn        = DateTime.Now,
                };
                _context.DictaatSession.Add(s);
                _context.SaveChanges();
            }

            var session = _context.DictaatSession
                          .Include("Participants.User")
                          .FirstOrDefault(s => s.DictaatDetailsId == dictaatName && s.EndedOn == null);

            var response = new ViewModels.Session(session);

            if (userId != null && response.ParticipantIds.Contains(userId))
            {
                response.ContainsMe = true;
            }
            return(response);
        }
Example #3
0
        // public async Task<IEnumerable<ViewModels.Session>> GetByUserIdAsync(Guid userId, CancellationToken ct)
        // {
        //     if (!(await _authorizationService.AuthorizeAsync(_user, null, new ContentDeveloperRequirement())).Succeeded)
        //         throw new ForbiddenException();

        //     var user = await _context.Users
        //         .Include(u => u.SessionMemberships)
        //             .ThenInclude(em => em.Session)
        //         .Where(u => u.Id == userId)
        //         .SingleOrDefaultAsync(ct);

        //     if (user == null)
        //         throw new EntityNotFoundException<User>();

        //     var Sessions = user.SessionMemberships.Select(x => x.Session);

        //     return _mapper.Map<IEnumerable<ViewModels.Session>>(Sessions);
        // }

        public async Task <ViewModels.Session> CreateAsync(ViewModels.Session session, CancellationToken ct)
        {
            if (!(await _authorizationService.AuthorizeAsync(_user, null, new ContentDeveloperRequirement())).Succeeded)
            {
                throw new ForbiddenException();
            }

            session.DateCreated = DateTime.UtcNow;
            session.CreatedBy   = _user.GetId();
            var sessionEntity = Mapper.Map <SessionEntity>(session);

            //TODO: add permissions
            // var SessionAdminPermission = await _context.Permissions
            //     .Where(p => p.Key == PlayerClaimTypes.SessionAdmin.ToString())
            //     .FirstOrDefaultAsync();

            // if (SessionAdminPermission == null)
            //     throw new EntityNotFoundException<Permission>($"{PlayerClaimTypes.SessionAdmin.ToString()} Permission not found.");

            _context.Sessions.Add(sessionEntity);
            await _context.SaveChangesAsync(ct);

            return(await GetAsync(sessionEntity.Id, ct));
        }