public async Task <bool> UploadImagesAsync([FromRoute] int id)
        {
            var files = Request.Form.Files;

            if (files.Count > 0)
            {
                var game = await _context.Game.FindAsync(id);

                if (game == null)
                {
                    throw new ApplicationException("Game is not existed");
                }

                var imageGames = await _context.ImageGame.Where(_ => _.GameId == id).ToListAsync();

                FileUploads fileUploads = new FileUploads();
                var         result      = new ImagePathsModel();

                foreach (var file in files)
                {
                    switch (file.Name)
                    {
                    case "banner":
                    {
                        result.PathBanner = fileUploads.UploadImage(file, "Banner_");

                        await DeleteOldPathImage(imageGames, file.Name, fileUploads);

                        await AddGameImage(file.Name, result.PathBanner, id);

                        break;
                    }

                    case "logo":
                    {
                        result.PathLogo = fileUploads.UploadImage(file, "Logo_");

                        await DeleteOldPathImage(imageGames, file.Name, fileUploads);

                        await AddGameImage(file.Name, result.PathLogo, id);

                        break;
                    }

                    default: break;
                    }

                    if (file.Name.Contains("description"))
                    {
                        var pathDesc = fileUploads.UploadImage(file, "Description_");
                        result.PathDescription.Add(pathDesc);

                        await DeleteOldPathImage(imageGames, file.Name, fileUploads);

                        await AddGameImage(file.Name, pathDesc, id);
                    }
                }

                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 + "}", "<img src=" + result.PathDescription[i] + " />");
                }

                await _context.SaveChangesAsync();
            }

            return(true);
        }
Esempio n. 2
0
        public async Task <bool> UploadImagesAsync([FromRoute] int id)
        {
            try
            {
                var files = Request.Form.Files;
                if (files.Count > 0)
                {
                    var news = await _context.News.FindAsync(id);

                    if (news == null)
                    {
                        throw new ApplicationException("News is not existed");
                    }

                    var imageNews = await _context.ImageNews.Where(_ => _.NewsId == id).ToListAsync();

                    FileUploads fileUploads = new FileUploads();
                    var         result      = new ImagePathsModel();

                    foreach (var file in Request.Form.Files)
                    {
                        if (file.Name == "logo")
                        {
                            result.PathLogo = fileUploads.UploadImage(file, "Logo_");

                            await DeleteOldPathImage(imageNews, file.Name, fileUploads);

                            await AddNewsImage(file.Name, result.PathLogo, id);
                        }

                        if (file.Name.Contains("description"))
                        {
                            var pathDesc = fileUploads.UploadImage(file, "Description_");
                            result.PathDescription.Add(pathDesc);

                            await DeleteOldPathImage(imageNews, file.Name, fileUploads);

                            await AddNewsImage(file.Name, pathDesc, id);
                        }
                    }

                    if (!string.IsNullOrEmpty(result.PathLogo))
                    {
                        news.Logo = result.PathLogo;
                    }

                    for (int i = 0; i < result.PathDescription.Count; i++)
                    {
                        news.Description = news.Description.Replace("{" + i + "}", "<img src=" + result.PathDescription[i] + " />");
                    }

                    await _context.SaveChangesAsync();
                }

                return(true);
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.Message);
            }
        }