コード例 #1
0
        public IActionResult Delete([FromRoute] string id)
        {
            var news = _newsService.Get(id);

            if (news == null)
            {
                return(NotFound());
            }
            FileUpload fileUpload = new FileUpload();

            fileUpload.DeleteImage(news.logo);
            if (news.images != null)
            {
                foreach (Image image in news.images)
                {
                    fileUpload.DeleteImage(image.dirPath);
                }
            }
            _newsService.Remove(news.id);

            return(NoContent());
        }
コード例 #2
0
        public IActionResult Delete([FromRoute] string id)
        {
            var game = _gameService.Get(id);

            if (game == null)
            {
                return(NotFound());
            }
            FileUpload fileUpload = new FileUpload();

            fileUpload.DeleteImage(game.logo);
            fileUpload.DeleteImage(game.banner);
            if (game.images != null)
            {
                foreach (Image image in game.images)
                {
                    fileUpload.DeleteImage(image.dirPath);
                }
            }

            _gameService.Remove(game.id);

            return(Ok());
        }
コード例 #3
0
        public bool UploadImages([FromRoute] string id)
        {
            var files = Request.Form.Files;

            if (files.Count > 0)
            {
                AddFolderStoringImage();

                var news = _newsService.Get(id);
                if (news != null)
                {
                    FileUpload   fileUpload = new FileUpload();
                    var          result     = new ImagePath();
                    List <Image> listImage  = new List <Image>();
                    Image        image;
                    foreach (var file in files)
                    {
                        if (file.Name == "logo")
                        {
                            result.pathLogo = fileUpload.UploadImage(file, "Logo_");
                            fileUpload.DeleteImage(news.logo);
                        }

                        if (file.Name.Contains("description"))
                        {
                            var pathDesc = fileUpload.UploadImage(file, "Description_");
                            result.pathDescription.Add(pathDesc);
                            image = new Image(Guid.NewGuid().ToString(), pathDesc);
                            listImage.Add(image);
                        }
                    }
                    news.images = listImage;
                    if (!string.IsNullOrEmpty(result.pathLogo))
                    {
                        news.logo = result.pathLogo;
                    }

                    for (int i = 0; i < result.pathDescription.Count; i++)
                    {
                        news.description = news.description.Replace("{" + i + "}", result.pathDescription[i]);
                    }
                    _newsService.Update(id, news);
                }
            }
            return(true);
        }
コード例 #4
0
        public bool UploadImages([FromRoute] string id)
        {
            try
            {
                var files = Request.Form.Files;
                if (files.Count > 0)
                {
                    AddFolderStoringImage();

                    var game = _gameService.Get(id);
                    if (game != null)
                    {
                        FileUpload   fileUpload = new FileUpload();
                        var          result     = new ImagePath();
                        List <Image> listImage  = new List <Image>();
                        Image        image;
                        foreach (var file in files)
                        {
                            switch (file.Name)
                            {
                            case "banner":
                            {
                                result.pathBanner = fileUpload.UploadImage(file, "Banner_");
                                fileUpload.DeleteImage(game.banner);
                                break;
                            }

                            case "logo":
                            {
                                result.pathLogo = fileUpload.UploadImage(file, "Logo_");
                                fileUpload.DeleteImage(game.logo);
                                break;
                            }

                            default: break;
                            }

                            if (file.Name.Contains("description"))
                            {
                                var pathDesc = fileUpload.UploadImage(file, "Description_");
                                result.pathDescription.Add(pathDesc);
                                image = new Image(Guid.NewGuid().ToString(), pathDesc);
                                listImage.Add(image);
                            }
                        }
                        game.images = listImage;
                        if (!string.IsNullOrEmpty(result.pathBanner))
                        {
                            game.banner = result.pathBanner;
                        }

                        if (!string.IsNullOrEmpty(result.pathLogo))
                        {
                            game.logo = result.pathLogo;
                        }

                        for (int i = 0; i < result.pathDescription.Count; i++)
                        {
                            game.description = game.description.Replace("{" + i + "}", result.pathDescription[i]);
                        }

                        _gameService.Update(id, game);
                    }
                }

                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }