Ejemplo n.º 1
0
        public ActionResult Create([FromForm] PostDto dto)
        {
            var ext = Path.GetExtension(dto.PostImage.FileName);

            if (!ImageFormatCheck.AllowedExtensions.Contains(ext))
            {
                return(UnprocessableEntity("Image extension is not allowed."));
            }

            try
            {
                var newFileName = Guid.NewGuid().ToString() + "_" + dto.PostImage.FileName;

                var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "PostImages", newFileName);

                dto.PostImage.CopyTo(new FileStream(filePath, FileMode.Create));

                var image = new CreatePostImageDto
                {
                    ImageName = newFileName
                };


                try
                {
                    this.imageId = _imageCreate.Execute(image);
                }
                catch (EntityAlreadyExistsException e) { TempData["Error"] = e.Message; }
                catch (Exception e) { TempData["Error"] = e.Message; }



                var post = new CreatePostDto
                {
                    Description = dto.Description,
                    Rating      = dto.Rating,
                    Title       = dto.Title,
                    UserId      = dto.UserId,
                    PostImageId = this.imageId
                };


                _postCreate.Execute(post);
                return(RedirectToAction(nameof(Index)));
            }
            catch (EntityAlreadyExistsException e) { TempData["Error"] = e.Message; }
            catch (Exception e) { TempData["Error"] = e.Message; }
            return(View());
        }
Ejemplo n.º 2
0
        public IActionResult Post([FromForm] AddPost p)
        {
            var ext = Path.GetExtension(p.Image.FileName);

            if (!FileUpload.AllowedExtensions.Contains(ext))
            {
                return(UnprocessableEntity("Image extension is not allowed."));
            }
            try
            {
                var newFileName = Guid.NewGuid().ToString() + "_" + p.Image.FileName;

                var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images", newFileName);
                p.Image.CopyTo(new FileStream(filePath, FileMode.Create));

                var post = new InsertPostDto
                {
                    Name       = p.Name,
                    FileName   = newFileName,
                    Text       = p.Text,
                    CategoryId = p.CategoryId,
                    UserId     = p.UserId,
                };
                _addPost.Execute(post);
                return(StatusCode(201));
            }

            catch (EntityAlredyExists)
            {
                return(NotFound());
            }

            catch (Exception e)
            {
                return(StatusCode(500, "An error has occured."));
            }
        }
Ejemplo n.º 3
0
        public void ExecuteCommand(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                throw new ArgumentNullException();
            }

            const string noPost    = "Nothing posted yet";
            const char   separator = ' ';

            string[] splitted = input.Split(separator);

            bool noActionInTheInput = splitted.Length == 1;

            var username = splitted[0];
            var action   = noActionInTheInput ? "read" : splitted[1];

            switch (action)
            {
            case "->":
                var message = string.Join(" ", splitted.Skip(2));
                _createPostCommand.Execute(username, message);
                break;

            case "follows":
                var usernameToFollow = splitted[2];
                _followUserCommand.Execute(username, usernameToFollow);
                break;

            case "wall":
                var wallPosts = _wallQuery.Execute(username);
                if (wallPosts.Any())
                {
                    wallPosts.ToList().ForEach(post =>
                                               _writer.Write($"{post.Username} - {post.Message} ({post.WhenPosted})"));
                }
                else
                {
                    _writer.Write(noPost);
                }
                break;

            case "read":
                var userPosts = _getPostListByUserQuery.Execute(username);
                if (userPosts.Any())
                {
                    userPosts.ToList()
                    .ForEach(post => _writer.Write($"{post.Message} ({post.WhenPosted})"));
                }
                else
                {
                    _writer.Write(noPost);
                }


                break;

            default:
                throw new Exception("Invalid command");
            }
        }