Exemplo n.º 1
0
        public IActionResult Add([FromForm] PostDTO model)
        {
            try
            {
                var currentUserId = User.FindFirst(ClaimTypes.NameIdentifier).Value;

                var guid = Guid.NewGuid();

                if (model.Photos.Count == 0)
                {
                    return(StatusCode(397));
                }

                if (model.Photos == null)
                {
                    return(StatusCode(398));
                }

                var images = new List <Image>();

                foreach (var file in model.Photos)
                {
                    var extention = Path.GetExtension(file.FileName);
                    if (extention == ".jpg" || extention == ".png")
                    {
                        var uploadResult = new ImageUploadResult();

                        using (var stream = file.OpenReadStream())
                        {
                            var uploadParams = new ImageUploadParams
                            {
                                File = new FileDescription(file.Name, stream),
                            };
                            uploadResult = _cloudinary.Upload(uploadParams);
                        }

                        images.Add(new Image
                        {
                            Id        = Guid.NewGuid(),
                            ImageUrls = uploadResult.Uri.ToString(),
                            PostId    = guid,
                            PublicId  = uploadResult.PublicId
                        });
                    }

                    if (extention == ".mp4")
                    {
                        var uploadvideoResult = new VideoUploadResult();
                        using (var stream = file.OpenReadStream())
                        {
                            var uploadParams = new VideoUploadParams()
                            {
                                File = new FileDescription(file.Name, stream)
                            };
                            uploadvideoResult = _cloudinary.Upload(uploadParams);
                        }

                        images.Add(new Image
                        {
                            Id        = Guid.NewGuid(),
                            ImageUrls = uploadvideoResult.Uri.ToString(),
                            PostId    = guid,
                            PublicId  = uploadvideoResult.PublicId
                        });
                    }
                }


                _imageService.AddRange(images);
                if (model.Steps != null)
                {
                    List <Step> steps = new List <Step>();
                    foreach (var step in model.Steps)
                    {
                        steps.Add(new Step {
                            Id = Guid.NewGuid(), PostId = guid, Description = step
                        });
                    }

                    _stepService.AddRange(steps);
                }

                if (model.Ingredients != null)
                {
                    List <Ingredients> ingredientses = new List <Ingredients>();
                    foreach (var ingredient in model.Ingredients)
                    {
                        ingredientses.Add(new Ingredients
                        {
                            Id = Guid.NewGuid(), PostId = guid, Ingredient = ingredient
                        });
                    }

                    _ingredientService.AddRange(ingredientses);
                }


                _postService.Add(new Post
                {
                    Id       = guid, Description = model.Description, UserId = Guid.Parse(currentUserId),
                    PostDate = DateTime.Now, LikeCount = 0, Title = model.Title, StarGivenUserCount = 0, SumStar = 0,
                    Star     = 0
                });


                return(Ok("Success"));
            }
            catch (Exception e)
            {
                return(StatusCode(399));
            }
        }