public async Task <bool> Handle(UpdateFlashCardCommand request, CancellationToken cancellationToken)
        {
            if (!(await IsFlashCardUserOwnAsync(request)))
            {
                return(false);
            }

            var entity = await _context.FlashCards.SingleOrDefaultAsync(f => f.FlashCardId == request.Id, cancellationToken : cancellationToken);

            if (entity == null)
            {
                throw new Exception($"{nameof(FlashCards)} not found with the Id {request.Id}");
            }

            entity.MainWord         = request.MainWord;
            entity.Example          = request.Example;
            entity.FlashCardPicture = request.Picture;
            entity.TypeCardId       = (int)request.TypeCard;
            entity.CategoryId       = request.Category;
            entity.Meaning          = request.Meaning;

            await _context.SaveChangesAsync(cancellationToken);



            return(true);
        }
예제 #2
0
        public async Task <CategoryDto> Handle(AddCategoryCommand request, CancellationToken cancellationToken)
        {
            var entity = new Category()
            {
                Description = request.Description
            };

            _context.Categories.Add(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(_mapper.Map <CategoryDto>(entity));
        }
        public async Task <Unit> Handle(UpdateCategoryCommand request, CancellationToken cancellationToken)
        {
            var entity = await _context.Categories.SingleOrDefaultAsync(c => c.CategoryId == request.Id);

            if (entity == null)
            {
                throw new Exception($"{typeof(Category)} entity does'nt exist with the id {request.Id}");
            }

            entity.Description = request.Description;
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(DeleteCategoryCommand request, CancellationToken cancellationToken)
        {
            var entity = await _context.Categories.FindAsync(request.Id);

            if (entity == null)
            {
                throw new Exception($"{typeof(Category)} not found with the Id {request.Id}");
            }

            _context.Categories.Remove(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <int> Handle(AddFlashCardCommand request, CancellationToken cancellationToken)
        {
            FlashCard entity = new FlashCard()
            {
                MainWord         = request.MainWord,
                CategoryId       = request.Category,
                Example          = request.Example,
                FlashCardPicture = request.Picture,
                TypeCardId       = (int)request.TypeCard,
                FlashCardDate    = DateTime.Now,
                Meaning          = request.Meaning,
                UserId           = _currentUserService.UserId
            };

            _context.FlashCards.Add(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(entity.FlashCardId);
        }
예제 #6
0
        public async Task <bool> Handle(DeleteFlashCardCommand request, CancellationToken cancellationToken)
        {
            if (!(await IsFlashCardUserOwnAsync(request)))
            {
                return(false);
            }

            var entity = await _context.FlashCards.FindAsync(request.Id);

            if (entity == null)
            {
                throw new Exception($"{nameof(FlashCards)} not found the entity with the Id {request.Id}");
            }

            _context.FlashCards.Remove(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(true);
        }
예제 #7
0
        public async Task <AuthenticationResult> RefreshTokenAsync(string token, string refreshToken)
        {
            var validatedToken = GetPrincipalFromToken(token);

            if (validatedToken == null)
            {
                return(new AuthenticationResult()
                {
                    Errors = new[] { "Invalid Token" }
                });
            }

            var expiryDateUnix =
                long.Parse(validatedToken.Claims.Single(x => x.Type == JwtRegisteredClaimNames.Exp).Value);

            var expiryDateTimeUtc = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
                                    .AddSeconds(expiryDateUnix);

            if (expiryDateTimeUtc > DateTime.UtcNow)
            {
                return(new AuthenticationResult()
                {
                    Errors = new[] { "This token hasn't expired yet" }
                });
            }
            var jti = validatedToken.Claims.Single(x => x.Type == JwtRegisteredClaimNames.Jti).Value;

            var storeRefreshtoken = await _context.RefreshTokens.SingleOrDefaultAsync(x => x.Token == refreshToken);

            if (storeRefreshtoken == null)
            {
                return(new AuthenticationResult
                {
                    Errors = new[] { "This refresh token doesn't exist" }
                });
            }

            if (DateTime.UtcNow > storeRefreshtoken.ExpiryDate)
            {
                return(new AuthenticationResult()
                {
                    Errors = new[] { "This refresh token has expired" }
                });
            }

            if (storeRefreshtoken.Invalidated)
            {
                return(new AuthenticationResult()
                {
                    Errors = new[] { "This refresh token has been invalidated" }
                });
            }

            if (storeRefreshtoken.Used)
            {
                return(new AuthenticationResult()
                {
                    Errors = new[] { "This refresh token has been used" }
                });
            }

            if (storeRefreshtoken.JwtId != jti)
            {
                return(new AuthenticationResult()
                {
                    Errors = new[] { "This refresh token does not match this Jwt" }
                });
            }

            storeRefreshtoken.Used = true;
            _context.RefreshTokens.Update(storeRefreshtoken);
            await _context.SaveChangesAsync(new CancellationToken());

            var user = await _userManager.FindByIdAsync(validatedToken.Claims.Single(x => x.Type == "id").Value);

            return(await GenerateAuthenticationResultForUserAsync(user));
        }