Beispiel #1
0
        public ActionResult GetComment(int id, int skip = 0, int take = 10)
        {
            if (skip < 0)
            {
                ApiErrorResponseBody errorResponse = new ApiErrorResponseBody(false);
                errorResponse.AddError("Skip", new string[] { "Skip cannot be negative" });
                return(BadRequest(errorResponse));
            }
            if (take < 1 || take > 100)
            {
                ApiErrorResponseBody errorResponse = new ApiErrorResponseBody(false);
                errorResponse.AddError("Take", new string[] { "Take needs to be a number between 1-100" });
                return(BadRequest(errorResponse));
            }
            if (!_unitOfWork.RealEstateRepository.EntityExists(id))
            {
                ApiErrorResponseBody errorResponse = new ApiErrorResponseBody(false);
                errorResponse.AddError("RealEstate", new string[] { $"Could not find a Real Estate with id {id}" });
                return(NotFound(errorResponse));
            }

            var comments = _unitOfWork.CommentRepository.GetCommentsForRealEstate(id, skip, take);
            var toReturn = _mapper.Map <List <CommentDto> >(comments);

            return(Ok(toReturn));
        }
Beispiel #2
0
        public ActionResult GetComment(string username, int skip = 0, int take = 10)
        {
            if (skip < 0)
            {
                ApiErrorResponseBody errorResponse = new ApiErrorResponseBody(false);
                errorResponse.AddError("Skip", new string[] { "Skip cannot be negative" });
                return(BadRequest(errorResponse));
            }
            if (take < 1 || take > 100)
            {
                ApiErrorResponseBody errorResponse = new ApiErrorResponseBody(false);
                errorResponse.AddError("Take", new string[] { "Take needs to be a number between 1-100" });
                return(BadRequest(errorResponse));
            }
            if (_unitOfWork.UserRepository.Get(username) == null)
            {
                ApiErrorResponseBody errorResponse = new ApiErrorResponseBody(false);
                errorResponse.AddError("Username", new string[] { $"Could not find a User with name {username}" });
                return(NotFound(errorResponse));
            }

            var comments = _unitOfWork.CommentRepository.GetCommentsByUser(username, skip, take);
            var toReturn = _mapper.Map <List <CommentDto> >(comments);

            return(Ok(toReturn));
        }
Beispiel #3
0
        public ActionResult PostComment(CommentForCreationDto commentForCreation)
        {
            //Gets username from the token
            var username = HttpContext.User.Identity.Name;
            var comment  = _mapper.Map <Comment>(commentForCreation);

            //Check if the realestate-id maps to an actual entity
            if (!_unitOfWork.RealEstateRepository.EntityExists(comment.RealEstateId))
            {
                ApiErrorResponseBody errorResponse = new ApiErrorResponseBody(false);
                errorResponse.AddError("RealEstate", new string[] { $"Could not find a Real Estate with id {comment.RealEstateId}" });
                return(NotFound(errorResponse));
            }

            //get current users Id, and adds it to the new comment
            comment.UserId = _unitOfWork.UserRepository.Get(username).Id;
            _unitOfWork.CommentRepository.Add(comment);
            _unitOfWork.SaveChanges();

            var commentToReturn = _mapper.Map <CommentDto>(comment);

            return(CreatedAtRoute(
                       "GetComment",
                       new { id = comment.RealEstateId },
                       commentToReturn));
        }
Beispiel #4
0
 public ActionResult GetCommentCountByUser(string username)
 {
     if (_unitOfWork.UserRepository.Get(username) == null)
     {
         ApiErrorResponseBody errorResponse = new ApiErrorResponseBody(false);
         errorResponse.AddError("Username", new string[] { $"Could not find a User with name {username}" });
         return(NotFound(errorResponse));
     }
     return(Ok(_unitOfWork.CommentRepository.GetCommentsByUsernameCount(username)));
 }
Beispiel #5
0
 public ActionResult GetCommentCountByEstate(int id)
 {
     if (!_unitOfWork.RealEstateRepository.EntityExists(id))
     {
         ApiErrorResponseBody errorResponse = new ApiErrorResponseBody(false);
         errorResponse.AddError("RealEstate", new string[] { $"Could not find a Real Estate with id {id}" });
         return(NotFound(errorResponse));
     }
     return(Ok(_unitOfWork.CommentRepository.GetCommentsByRealEstateCount(id)));
 }
Beispiel #6
0
 public async Task <IActionResult> Create(string username, string password, string grant_type)
 {
     if (await IsValidUsernameAndPassword(username, password))
     {
         return(Ok(new ObjectResult(await GenerateToken(username))));
     }
     else
     {
         ApiErrorResponseBody errorResponse = new ApiErrorResponseBody(false);
         errorResponse.AddError("Username/Password", new string[] { "Invalid username or password" });
         return(BadRequest(errorResponse));
     }
 }
        public ActionResult GetRealEstateNoAuth(int id)
        {
            var realEstateEntity = _unitOfWork.RealEstateRepository.GetWithIncludes(id);

            if (realEstateEntity == null)
            {
                ApiErrorResponseBody errorResponse = new ApiErrorResponseBody(false);
                errorResponse.AddError("RealEstate", new string[] { $"Could not find a Real Estate with id {id}" });
                return(NotFound(errorResponse));
            }

            var realEstateDto = _mapper.Map <RealEstateNoAuthDetailDto>(realEstateEntity);

            return(Ok(realEstateDto));
        }
        public ActionResult GetUser(string userName)
        {
            var user = _unitOfWork.UserRepository.Get(userName);

            if (user == null)
            {
                ApiErrorResponseBody errorResponse = new ApiErrorResponseBody(false);
                errorResponse.AddError("User", new string[] { $"Could not found a user with name {userName}" });
                return(NotFound(errorResponse));
            }

            var userForReturn = _mapper.Map <UserDto>(user);

            return(Ok(userForReturn));
        }
        public async Task <IActionResult> RegisterNewUser([FromForm] UserForCreationDto userForCreation)
        {
            var user   = _mapper.Map <User>(userForCreation);
            var result = await _userManager.CreateAsync(user, userForCreation.Password);

            if (!result.Succeeded)
            {
                //Create an errorResponeBody, and add all errors found in userManagers result
                ApiErrorResponseBody errorResponse = new ApiErrorResponseBody(false);
                foreach (var error in result.Errors)
                {
                    errorResponse.AddError(error.Code, new string[] { error.Description });
                }
                return(BadRequest(errorResponse));
            }
            return(Ok());
        }
        public IActionResult GetRealEstates(int skip = 0, int take = 10, string city = "")
        {
            if (skip < 0)
            {
                ApiErrorResponseBody errorResponse = new ApiErrorResponseBody(false);
                errorResponse.AddError("Skip", new string[] { "Skip cannot be negative" });
                return(BadRequest(errorResponse));
            }
            if (take < 1 || take > 100)
            {
                ApiErrorResponseBody errorResponse = new ApiErrorResponseBody(false);
                errorResponse.AddError("Take", new string[] { "Take needs to be a number between 1-100" });
                return(BadRequest(errorResponse));
            }

            var realEstateEntities = _unitOfWork.RealEstateRepository
                                     .SkipAndTakeRealEstatesByCity(skip, take, city);
            var realEstateDtos = _mapper.Map <IEnumerable <RealEstateDto> >(realEstateEntities);

            return(Ok(realEstateDtos));
        }
        public async Task <ActionResult> RateUser(RatingForCreationDto rating)
        {
            var ratedUser = _unitOfWork.UserRepository.Get(rating.UserName);

            if (ratedUser == null)
            {
                ApiErrorResponseBody errorResponse = new ApiErrorResponseBody(false);
                errorResponse.AddError("User", new string[] { $"Could not found a user with name {rating.UserName}" });
                return(NotFound(errorResponse));
            }

            var ratingUser = await _userManager.GetUserAsync(User);

            if (ratingUser.Id == ratedUser.Id)
            {
                ApiErrorResponseBody errorResponse = new ApiErrorResponseBody(false);
                errorResponse.AddError("IllicitRating", new string[] { "A user can't give itself a rating" });
                return(BadRequest(errorResponse));
            }

            var foundRating = _unitOfWork.RatingRepository.Get(ratedUser.Id, ratingUser.Id);

            if (foundRating == null)
            {
                var ratingToAdd = _mapper.Map <Rating>(rating);
                ratingToAdd.RatingUserId = ratingUser.Id;
                ratingToAdd.RatedUserId  = ratedUser.Id;
                _unitOfWork.RatingRepository.Add(ratingToAdd);
            }
            else
            {
                foundRating.Score = rating.Value;
            }

            _unitOfWork.SaveChanges();

            return(Ok());
        }