Пример #1
0
        public async Task <IActionResult> Search([FromRoute] string title, string page)
        {
            var result = new MovieListResponse
            {
                Page    = ConvertUtils.ToInt(page) > 0 ? ConvertUtils.ToInt(page) : 1,
                PerPage = 10
            };

            var skip = (result.Page - 1) * result.PerPage;

            try
            {
                var repository = base.repository as IMoviesRepository;
                var rows       = await repository.Search(title);

                result.Total      = rows.Count();
                result.TotalPages = (result.Total / result.PerPage) + (result.Total % result.PerPage == 0 ? 0 : 1);
                result.Data       = rows.Skip(skip).Take(result.PerPage).ToList().Select(s => MovieResponse.FromModel(s)).ToList();
                result.Success    = true;

                return(Ok(result));
            }
            catch (ApplicationException ex)
            {
                LogUtils.Add("MoviesController.Search", ex);
                result.Message = ex.Message;
                result.Success = false;
                return(BadRequest(result));
            }
            catch (UnauthorizedAccessException ex)
            {
                LogUtils.Add("MoviesController.Search", ex);
                result.Message = this.OOPS_ACCESS_DENIED;
                result.Success = false;
                return(Unauthorized(result));
            }
            catch (Exception ex)
            {
                LogUtils.Add("MoviesController.Search", ex);
                result.Message = base.OOPS_ERROR_MESSAGE;
                result.Success = false;
                return(NotFound(result));
            }
        }
Пример #2
0
        public async Task <IActionResult> Authenticate([FromBody] Models.Request.LoginRequest request)
        {
            var result = new TokenResponse();

            try
            {
                #region .: request validation :.

                if (request == null)
                {
                    throw new ApplicationException("Request cannot be null!");
                }
                if (string.IsNullOrEmpty(request.Login))
                {
                    throw new ApplicationException("Login cannot be null");
                }
                if (string.IsNullOrEmpty(request.Password))
                {
                    throw new ApplicationException("Password cannot be null");
                }
                var user = await _userRepository.FindByLogin(request.Login);

                if (user == null)
                {
                    throw new ApplicationException($"User {request.Login} not found!");
                }

                var password = SecurityUtils.Encrypt(request.Password, user.Saltkey);
                if (!user.Password.Equals(password))
                {
                    throw new ApplicationException($"Invalid Login or Password!");
                }

                #endregion

                var token = new Domain.Models.Tokens
                {
                    Token        = Guid.NewGuid().ToString(),
                    UserId       = user.Id,
                    DtExpiration = DateTime.UtcNow.AddDays(1),
                    Active       = true,
                    DtCreated    = DateTime.UtcNow
                };

                await base.InternalPost(token);

                return(Ok(TokenResponse.FromModel(token, user)));
            }
            catch (ApplicationException ex)
            {
                LogUtils.Add("TokensController.Authenticate", ex);
                result.Message = ex.Message;
                result.Success = false;
                return(BadRequest(result));
            }
            catch (UnauthorizedAccessException ex)
            {
                LogUtils.Add("TokensController.Authenticate", ex);
                result.Message = this.OOPS_ACCESS_DENIED;
                result.Success = false;
                return(Unauthorized(result));
            }
            catch (Exception ex)
            {
                LogUtils.Add("TokensController.Authenticate", ex);
                result.Message = this.OOPS_ERROR_MESSAGE;
                result.Success = false;
                return(NotFound(result));
            }
        }