public async Task <IActionResult> UploadImage(ImageUploadViewModel image, IFormFile file)
        {
            if (ModelState.IsValid)
            {
                if (image.VenueId == null)
                {
                    throw new Exception("Venue id cannot be null");
                }

                try {
                    if (file != null)
                    {
                        var imageDb = new CovidOutApp.Web.Models.Image();
                        imageDb.Id    = Guid.NewGuid();
                        imageDb.Name  = image.Title;
                        imageDb.Venue = this._venueService.GetVenueById(image.VenueId);

                        bool successImageStored = await this._venueService.AddImageAsync(imageDb, file, image.IsLogoImage);
                    }
                    return(new RedirectToActionResult("Images", "Venue", new { id = image.VenueId }));
                }
                catch (Exception ex) {
                    this._logger.LogError(ex.StackTrace);
                    ModelState.AddModelError("Error", ex.Message);
                }
            }

            return(View(image));
        }
Example #2
0
        public async Task <bool> AddImageAsync(CovidOutApp.Web.Models.Image imageMetadata, IFormFile imageFile, bool isLogo)
        {
            var imageStored = false;

            if (imageMetadata == null)
            {
                throw new Exception("Image cannot be null");
            }

            try
            {
                if (String.IsNullOrEmpty(Globals.FILE_UPLOAD_DIR))
                {
                    throw new Exception("The file upload dir is not specified");
                }

                var filepath = await SaveImageToFile(imageFile, Globals.FILE_UPLOAD_DIR);

                if (String.IsNullOrEmpty(imageMetadata.ImagePath))
                {
                    var fileName = Path.GetFileName(filepath);

                    var relativePath = $"/{Path.GetFileName(Globals.FILE_UPLOAD_DIR)}/{fileName}";

                    imageMetadata.ImagePath = relativePath;
                }

                if (isLogo)
                {
                    var venue = this._venueRepository.Find(imageMetadata.Venue.Id);
                    venue.Logo = imageMetadata.ImagePath;
                    this._venueRepository.Update(venue);
                }
                else
                {
                    this._venueImageRepository.Add(imageMetadata);
                }

                imageStored = true;
            }
            catch (System.Exception ex)
            {
                _logger.LogError(ex.StackTrace);
                throw;
            }

            return(imageStored);
        }