public PostDto CreatePost(PostCreationDto post)
        {
            if (userMockRepository.GetUserByID(post.UserId) == null)
            {
                throw new _404Exception("User not found!");
            }
            if (!typeRepository.ContainsType(post.Type))
            {
                throw new _404Exception("Type not found!");
            }

            var newPost = autoMapper.Map <Post>(post);

            newPost.PostPublishingDateTime = DateTime.Now;
            newPost.LastModified           = DateTime.Now;
            newPost.PostTypeId             = typeRepository.GetIdByType(post.Type);
            try
            {
                var addedPost = postRepository.CreatePost(newPost);
                return(autoMapper.Map <PostDto>(addedPost));
            } catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Ejemplo n.º 2
0
        public RatingDTO CreateRating(RatingCreationDTO rating, int userId)
        {
            var user = _userMockRepository.GetUserByID(userId);

            if (user == null)
            {
                throw new NotFoundException("Not find user with that ID found...");
            }

            if (_postMockRepository.GetPostById(rating.PostID) == null)
            {
                throw new NotFoundException("Post with that ID does not exist!");
            }

            if (_ratingTypeRepository.GetRatingTypeByID(rating.RatingTypeID) == null)
            {
                throw new NotFoundException("Type of rating with that ID does not exist!");
            }

            Rating entity = mapper.Map <Rating>(rating);

            entity.UserID     = userId;
            entity.RatingDate = DateTime.Now;

            var post             = _postMockRepository.GetPostById(rating.PostID);
            var userThatPostedId = post.UserID;

            if (_ratingRepository.CheckDidIBlockUser(userId, userThatPostedId))
            {
                throw new BlockingException("You have blocked this user and you can not rate to his posts.");
            }

            if (!_ratingRepository.CheckDoIFollowUser(userId, userThatPostedId))
            {
                throw new BlockingException("You are not following this user and you can not rate to his posts.");
            }

            if (_ratingRepository.CheckDidIAlreadyRate(userId, rating.PostID) != null)
            {
                throw new ErrorOccurException("You have already rate to this post.");
            }

            try
            {
                var rate = _ratingRepository.CreateRating(entity);
                _ratingRepository.SaveChanges();
                return(mapper.Map <RatingDTO>(rate));
            }
            catch (Exception ex)
            {
                throw new ErrorOccurException(ex.Message);
            }
        }
        public ForumMessageDTO CreateForumMessage(ForumMessageCreateDTO newForumMessage)
        {
            ForumMessage entity = mapper.Map <ForumMessage>(newForumMessage);
            var          user   = _userMockRepository.GetUserByID(entity.SenderID);

            if (user == null)
            {
                throw new NotFoundException("There is no user with that ID!");
            }

            try
            {
                var forumMessage = _forumMessageRepository.CreateForumMessage(entity);
                _forumMessageRepository.SaveChanges();
                return(mapper.Map <ForumMessageDTO>(forumMessage));
            }
            catch (Exception ex)
            {
                throw new ErrorOccurException(ex.Message);
            }
        }
        public List <ForumDTO> GetForumsByOwner(int ownerID)
        {
            var userId = _userMockRepository.GetUserByID(ownerID);

            if (userId == null)
            {
                throw new NotFoundException("There is no user with that ID ...");
            }

            var forums = _forumRepository.GetForumsByOwner(ownerID);

            if (forums == null || forums.Count == 0)
            {
                throw new ErrorOccurException("This user has not yet created any forum...");
            }

            return(mapper.Map <List <ForumDTO> >(forums));
        }
Ejemplo n.º 5
0
        public MessageDTO CreateMessage(MessageCreateDTO newMessage, int senderID)
        {
            var user = _userMockRepository.GetUserByID(senderID);

            if (user == null)
            {
                throw new NotFoundException("User with passed ID is not found...");
            }

            Message entity = mapper.Map <Message>(newMessage);

            entity.SenderID = senderID;

            if (_messageRepository.CheckDidIBlockUser(senderID, entity.ReceiverID))
            {
                throw new BlockingException("You have blocked this user and you can not send him message.");
            }


            if (!_messageRepository.CheckDoIFollowUser(senderID, entity.ReceiverID))
            {
                throw new BlockingException("You are not following this user and you can not send him message.");
            }

            try
            {
                entity.IsSent = true;
                var message = _messageRepository.CreateMessage(entity);
                _messageRepository.SaveChanges();
                return(mapper.Map <MessageDTO>(message));
            }
            catch (Exception ex)
            {
                throw new ErrorOccurException(ex.Message);
            }
        }
Ejemplo n.º 6
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);
            }
        }