public async Task <GearItemViewModel> AddGearItemAsync(GearItemViewModel gearItemViewModel, CancellationToken ct = default)
        {
            List <GearImageViewModel> gearImages = new List <GearImageViewModel>();
            List <GearSizeViewModel>  gearSizes  = new List <GearSizeViewModel>();

            GearItem newGearItem = new GearItem()
            {
                Name    = gearItemViewModel.Name,
                Price   = gearItemViewModel.Price,
                InStock = gearItemViewModel.InStock
            };

            newGearItem = await _gearItemRepository.AddAsync(newGearItem, ct);

            gearItemViewModel.Id = newGearItem.Id;

            foreach (GearImageViewModel gearImageViewModel in gearItemViewModel.Images)
            {
                gearImageViewModel.GearItemId = gearItemViewModel.Id;

                gearImages.Add(await AddGearImageAsync(gearImageViewModel));
            }

            foreach (GearSizeViewModel gearSizeViewModel in gearItemViewModel.Sizes)
            {
                gearSizeViewModel.GearItemId = gearItemViewModel.Id;

                gearSizes.Add(await AddGearSizeAsync(gearSizeViewModel));
            }

            gearItemViewModel.Images = gearImages;
            gearItemViewModel.Sizes  = gearSizes;

            return(gearItemViewModel);
        }
        public async Task <bool> DeleteGearItemAsync(long?gearItemId, CancellationToken ct = default)
        {
            GearItemViewModel gearItemToDelete = GearItemConverter.Convert(await this._gearItemRepository.GetByIDAsync(gearItemId, ct));

            if (gearItemToDelete == null)
            {
                return(false);
            }

            if (gearItemToDelete.Images.Count() > 0)
            {
                // Delete gearImages from database
                foreach (GearImageViewModel gearImage in gearItemToDelete.Images)
                {
                    await this._gearImageRepository.DeleteAsync(gearImage.Id, ct);
                }
            }

            foreach (GearSizeViewModel size in gearItemToDelete.Sizes)
            {
                await this._gearSizeRepository.DeleteAsync(size.Id, ct);
            }

            return(await this._gearItemRepository.DeleteAsync(gearItemId, ct));
        }
        public async Task <ActionResult <GearItemViewModel> > Update([FromForm] List <IFormFile> gearImages)
        {
            // Retrieve gearItem object from the request form
            GearItemViewModel gearItem = JsonConvert.DeserializeObject <GearItemViewModel>(Request.Form["gearItem"]);

            // Check if we are uploading any new images
            if (gearImages.Count() > 0)
            {
                IList <GearImageViewModel> gearItemImages = await this._cloudinary.UploadNewImages <GearImageViewModel>(gearImages);

                if (gearItemImages.Any(gI => gI == null))
                {
                    return(BadRequest(Errors.AddErrorToModelState(ErrorCodes.CloudinaryUpload, ErrorDescriptions.CloudinaryImageUploadFailure, ModelState)));
                }

                gearItem.NewImages = gearItemImages;
            }

            if (!await this._supervisor.UpdateGearItemAsync(gearItem))
            {
                return(BadRequest(Errors.AddErrorToModelState(ErrorCodes.GearItemUpdate, ErrorDescriptions.GearItemUpdateFailure, ModelState)));
            }

            gearItem = await this._supervisor.GetGearItemByIdAsync(gearItem.Id);

            return(new OkObjectResult(gearItem));
        }
        public async Task <ActionResult <bool> > Delete(long id)
        {
            //Check if gear item exists
            GearItemViewModel gearItemToDelete = await this._supervisor.GetGearItemByIdAsync(id);

            if (gearItemToDelete == null)
            {
                return(BadRequest(Errors.AddErrorToModelState(ErrorCodes.GearItemNotFound, ErrorDescriptions.GearItemDeleteFailure, ModelState)));
            }

            if (gearItemToDelete.Images.Count() > 0)
            {
                DelResResult deletedFromCloudinary = await this._cloudinary.DeleteResources(gearItemToDelete);

                if (deletedFromCloudinary.StatusCode != HttpStatusCode.OK)
                {
                    return(BadRequest(Errors.AddErrorToModelState(ErrorCodes.CloudinaryDelete, ErrorDescriptions.CloudinaryImageDeleteFailure, ModelState)));
                }
            }

            if (!await _supervisor.DeleteGearItemAsync(id))
            {
                return(BadRequest(Errors.AddErrorToModelState(ErrorCodes.GearItemDelete, ErrorDescriptions.GearItemDeleteFailure, ModelState)));
            }

            return(new OkObjectResult(true));
        }
Exemple #5
0
        public bool SendEmail(PreOrderViewModel email, GearItemViewModel gearItemPreOrder)
        {
            bool success = false;

            try
            {
                // Create the template class
                PreOrderTemplate preOrderTemplate = new PreOrderTemplate(this._configuration);

                // Message to User
                MimeMessage messageToUser = new MimeMessage();
                // Add From
                messageToUser.From.Add(new MailboxAddress(this._emailAppSettings[nameof(EmailServiceOptions.Admin)], this._emailAppSettings[nameof(EmailServiceOptions.AdminEmail)]));
                // Add TO
                messageToUser.To.Add(new MailboxAddress(User, email.Contact.Email));
                // Add Subject
                messageToUser.Subject = preOrderTemplate.SubjectForUser(gearItemPreOrder.Name);

                BodyBuilder bodyBuilderForUser = new BodyBuilder();

                bodyBuilderForUser.HtmlBody = preOrderTemplate.UserEmailBody(email, gearItemPreOrder);

                messageToUser.Body = bodyBuilderForUser.ToMessageBody();

                // Message To Admin
                MimeMessage messageToAdmin = new MimeMessage();
                // Add From
                messageToAdmin.From.Add(new MailboxAddress(this._emailAppSettings[nameof(EmailServiceOptions.Admin)], this._emailAppSettings[nameof(EmailServiceOptions.SystemAdminEmail)]));
                // Add TO
                messageToAdmin.To.Add(new MailboxAddress(this._emailAppSettings[nameof(EmailServiceOptions.Admin)], this._emailAppSettings[nameof(EmailServiceOptions.AdminEmail)]));
                // Add Subject
                messageToAdmin.Subject = preOrderTemplate.SubjectForAdmin(email.Id);

                BodyBuilder bodyBuilderForAdmin = new BodyBuilder();

                bodyBuilderForAdmin.HtmlBody = preOrderTemplate.AdminEmailBody(email, gearItemPreOrder);

                messageToAdmin.Body = bodyBuilderForAdmin.ToMessageBody();

                SmtpClient client = new SmtpClient();
                client.Connect(this._emailAppSettings[nameof(EmailServiceOptions.SmtpServer)], int.Parse(this._emailAppSettings[nameof(EmailServiceOptions.Port)]), true);
                client.Authenticate(this._emailAppSettings[nameof(EmailServiceOptions.SystemAdminEmail)], this._configuration[nameof(VaultKeys.SystemAdminEmailPassword)]);

                client.Send(messageToUser);
                client.Send(messageToAdmin);

                client.Disconnect(true);
                client.Dispose();

                success = true;
            }
            catch
            {
                success = false;
            }

            return(success);
        }
        public async Task <GearItemViewModel> GetGearItemByIdAsync(long?gearItemId, CancellationToken ct = default)
        {
            GearItemViewModel gearitemViewModel = GearItemConverter.Convert(await this._gearItemRepository.GetByIDAsync(gearItemId, ct));

            if (gearitemViewModel == null)
            {
                return(null);
            }
            gearitemViewModel.Images = await GetAllGearImagesByGearItemIdAsync(gearItemId, ct);

            gearitemViewModel.Sizes = await GetAllGearSizesByGearItemIdAsync(gearItemId, ct);

            return(gearitemViewModel);
        }
Exemple #7
0
        public IActionResult Index(string categoryName, string categoryDesc, int id)
        {
            var gearitem = _gearItemRepository.GearItemsByCategoryId(id).OrderBy(g => g.Name);


            var gearItemViewModel = new GearItemViewModel()
            {
                GearItems           = gearitem.ToList(),
                CategoryName        = categoryName,
                CategoryDescription = categoryDesc
            };

            return(View(gearItemViewModel));
        }
Exemple #8
0
    public async Task <DelResResult> DeleteResources(GearItemViewModel gearItem)
    {
        DelResResult result = null;

        foreach (GearImageViewModel gearImage in gearItem.Images)
        {
            result = await this.DeleteResource(gearImage.CloudinaryPublicId);

            if (result.StatusCode != HttpStatusCode.OK)
            {
                return(result);
            }
        }
        return(result);
    }
        public static List <GearItemViewModel> ConvertList(IEnumerable <GearItem> gearItems)
        {
            return(gearItems.Select(gearItem =>
            {
                GearItemViewModel gearItemViewModel = new GearItemViewModel();
                gearItemViewModel.Id = gearItem.Id;
                gearItemViewModel.Images = GearImageConverter.ConvertList(gearItem.Images);
                gearItemViewModel.InStock = gearItem.InStock;
                gearItemViewModel.Name = gearItem.Name;
                gearItemViewModel.Price = gearItem.Price;
                gearItemViewModel.Sizes = GearSizeConverter.ConvertList(gearItem.Sizes);

                return gearItemViewModel;
            }).ToList());
        }
        public string AdminEmailBody(PreOrderViewModel email, GearItemViewModel gearItem)
        {
            decimal total = Math.Round(email.Quantity.GetValueOrDefault() * gearItem.Price, 2);

            return($@"<html lang='en'>
<head>     
  <meta charset='utf-8'>
  <meta name='viewport' content='initial-scale=1, maximum-scale=1'>
  <meta name='viewport' content='width=device-width, initial-scale=1'>  
  <link href='https://fonts.googleapis.com/css?family=Roboto:300,400,500' rel='stylesheet'>  
  <link href='https://fonts.googleapis.com/css?family=Roboto&display=swap' rel='stylesheet'>
  </style>
</head>
<body style='height: 100vh; font-family: 'Roboto', sans-serif; color:white; text-align: center'>   
    <div>            
        <img src='https://res.cloudinary.com/dwsvaiiox/image/upload/v1562171421/movies-place/itmfvw7fogexa8xqyzbg.png' style='max-width:100%; max-height:185px; margin: 0 auto; display: block'>
      </div>
    <div style='text-align: center'>
        <h3>Pre-order has been placed for {email.Quantity} {gearItem.Name}. The total is ${total}. Order Details:</h3>
    </div>
        <div style='margin-bottom: 10%'>
          <ul>
            <li>Item ID: {gearItem.Id}</li>
            <li>Size: {(Size)email.Size}</li>
            <li>Quantity: {email.Quantity}</li>
            <li>Price: ${Math.Round(gearItem.Price, 2)}</li>            
            <li>Name: {email.Contact.FirstName} {email.Contact.LastName}</li>
            <li>Phone Number: {email.Contact.PhoneNumber}</li>
            <li>E-Mail: {email.Contact.Email}</li>
            <li>Preferred Form of Contact: {(PreferredContact)email.Contact.PreferredContact}</li>            
          </ul>
        </div>
        <div>            
            <img src='{gearItem.Images[0].Url}' style='max-width:100%; max-height:250px; margin: 0 auto; display: block'>
          </div>
        <br>
        <div style='text-align: center; margin-bottom:15%'>
          
            <h4 style='font-weight: 900'>
                {_emailAppSettings[nameof(EmailServiceOptions.AdminEmail)]}
            </h4>
          <h4 style='font-weight: 900;'>
            {_emailAppSettings[nameof(EmailServiceOptions.AdminPhoneNumber)]}
          </h4>          
        </div>
</body>
</html>");
        }
        public async Task <bool> UpdateGearItemAsync(GearItemViewModel gearItemViewModel, CancellationToken ct = default)
        {
            GearItem gearItem = await _gearItemRepository.GetByIDAsync(gearItemViewModel.Id, ct);

            if (gearItem == null)
            {
                return(false);
            }

            gearItem.Name    = gearItemViewModel.Name;
            gearItem.InStock = gearItemViewModel.InStock;
            gearItem.Price   = gearItemViewModel.Price;

            // If incoming gearItemViewModel image count is smaller than existing, this means we are removing images
            if (gearItemViewModel.Images.Count() < gearItem.Images.Count())
            {
                IList <GearImage> deleteGearImages = gearItem.Images.Where(existingGearImage => !gearItemViewModel.Images.Any(updatedGearImage => updatedGearImage.Id == existingGearImage.Id)).ToList();

                // Delete all images marked for deletion
                int length = deleteGearImages.Count();
                for (int i = 0; i < length; i++)
                {
                    GearImage gearImage = deleteGearImages.ElementAt(i);
                    await this._gearImageRepository.DeleteAsync(gearImage.Id, ct);
                }
            }

            // Check if we are adding any newImages. If we arent this means the gearItem will have NO default image
            if (gearItemViewModel.NewImages != null)
            {
                // Add all new incoming images
                foreach (GearImageViewModel gearImage in gearItemViewModel.NewImages)
                {
                    gearImage.GearItemId = gearItem.Id;
                    await this.AddGearImageAsync(gearImage, ct);
                }
            }

            // Update incoming sizes
            foreach (GearSizeViewModel gearSize in gearItemViewModel.Sizes)
            {
                await this.UpdateGearSizeAsync(gearSize, ct);
            }

            return(await this._gearItemRepository.UpdateAsync(gearItem));
        }
        public static GearItemViewModel Convert(GearItem gearItem)
        {
            if (gearItem == null)
            {
                return(null);
            }
            GearItemViewModel gearItemViewModel = new GearItemViewModel();

            gearItemViewModel.Id      = gearItem.Id;
            gearItemViewModel.Images  = GearImageConverter.ConvertList(gearItem.Images);
            gearItemViewModel.InStock = gearItem.InStock;
            gearItemViewModel.Name    = gearItem.Name;
            gearItemViewModel.Price   = gearItem.Price;
            gearItemViewModel.Sizes   = GearSizeConverter.ConvertList(gearItem.Sizes);

            return(gearItemViewModel);
        }
        public async Task <ActionResult <PreOrderViewModel> > PreOrder(PreOrderViewModel preOrderForm, CancellationToken ct = default(CancellationToken))
        {
            GearItemViewModel gearItem = await this._supervisor.GetGearItemByIdAsync(preOrderForm.GearItemId);

            if (gearItem == null)
            {
                return(BadRequest(Errors.AddErrorToModelState(ErrorCodes.GearItemNotFound, ErrorDescriptions.GearItemFindFailure, ModelState)));
            }

            preOrderForm = await this._supervisor.AddPreOrderAsync(preOrderForm, ct);

            //Send an email out to let him know new pre-order has come in
            if (!this._emailService.SendEmail(preOrderForm, gearItem))
            {
                return(BadRequest(Errors.AddErrorToModelState(ErrorCodes.SendEmail, ErrorDescriptions.SendEmailFailure, ModelState)));
            }

            return(preOrderForm);
        }
        public async Task <ActionResult <GearItemViewModel> > Create([FromForm] List <IFormFile> gearImages)
        {
            // Retrieve gearItem object from the request form
            GearItemViewModel gearItem = JsonConvert.DeserializeObject <GearItemViewModel>(Request.Form["gearItem"]);

            //Check if we're uploading images
            if (gearImages.Count() > 0)
            {
                // IList of IFormFile for incoming images to be uploaded
                IList <GearImageViewModel> gearItemImages = await this._cloudinary.UploadNewImages <GearImageViewModel>(gearImages);

                if (gearItemImages.Any(gI => gI == null))
                {
                    return(BadRequest(Errors.AddErrorToModelState(ErrorCodes.CloudinaryUpload, ErrorDescriptions.CloudinaryImageUploadFailure, ModelState)));
                }

                gearItem.Images = gearItemImages;
            }
            // If we are no uploading images use default image
            else
            {
                gearItem.Images = new GearImageViewModel[]
                {
                    new GearImageViewModel
                    {
                        Url    = GearImageViewModel.DefaultGearItemImageUrlBig,
                        Small  = GearImageViewModel.DefaultGearItemImageUrlSmall,
                        Medium = GearImageViewModel.DefaultGearItemImageUrlMedium,
                        Big    = GearImageViewModel.DefaultGearItemImageUrlBig,
                        Name   = "PlaceholderGearPhoto"
                    }
                };
            }

            // Persist the gearItem along with its successfully uploaded cloudinary images to the database
            gearItem = await this._supervisor.AddGearItemAsync(gearItem);

            return(new JsonResult(gearItem));
        }