Beispiel #1
0
        public BlockDTO Block(BlockCreationDTO block, int blockerID, int blockedID)
        {
            if (_userMockRepository.GetUserByID(blockedID) == null)
            {
                throw new NotFoundException("User with that ID does not exist!");
            }

            if (_userMockRepository.GetUserByID(blockerID) == null)
            {
                throw new NotFoundException("User with that ID does not exist!");
            }

            if (blockedID == blockerID)
            {
                throw new ErrorOccurException("Error! Can not block yourself!");
            }

            if (!_blockingRepository.CheckDoIFollowUser(blockerID, blockedID))
            {
                throw new FollowingException("You dont follow user with that ID, so you can not block him!");
            }

            if (_blockingRepository.CheckDidIAlreadyBlockUser(blockerID, blockedID))
            {
                throw new BlockingException("You already blocked this user, you are not following him!");
            }

            Block type = mapper.Map <Block>(block);

            type.BlockDate = DateTime.Now;

            try
            {
                var created = _blockingRepository.Block(type);
                _blockingRepository.SaveChanges();
                return(mapper.Map <BlockDTO>(created));
            }
            catch (Exception ex)
            {
                throw new ErrorOccurException(ex.Message);
            }
        }
Beispiel #2
0
        public ActionResult <BlockDTO> Block([FromHeader] string key, [FromBody] BlockCreationDTO block, int blockerID, int blockedID)
        {
            if (!_authService.Authorize(key))
            {
                return(StatusCode(StatusCodes.Status401Unauthorized, "User authorization failed!"));
            }

            try
            {
                var createdType = _blockingService.Block(block, blockerID, blockedID);


                //string location = linkGenerator.GetPathByAction("GetBlockByID", "Block", new { BlockID = createdType.BlockID });

                return(StatusCode(StatusCodes.Status201Created, "You have successfully blocked user"));
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Error creating new block: " + ex.Message);

                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }