Exemple #1
0
        public async Task <CakeOrder> AddAsync(CakeOrder cakeOrder, CancellationToken ct = default(CancellationToken))
        {
            _context.CakeOrders.Add(cakeOrder);
            await _context.SaveChangesAsync(ct);

            return(cakeOrder);
        }
        public async Task <CakeOrder> UpdateAsync(CakeOrder cakeOrder)
        {
            var entity = _context.CakeOrders.Update(cakeOrder);
            await _context.SaveChangesAsync();

            return(entity.Entity);
        }
Exemple #3
0
        //public async Task<Response> SendEmail(CakeOrder cakeOrder) // NotificationHandler oluşturuken değiştirdik. Önceden bir email gönderince send grid ten aldığımız response yeterliydi. şimdi ServiceResponse döndüreceğiz ve bunu NotificationRules/AcceptedNotificationRule içinde kullanacağız
        public async Task <ServiceResponse> SendEmail(CakeOrder cakeOrder)
        {
            var email   = CreateEmail(cakeOrder); // aşağıdaki method da email oluşturacağız.
            var message = MailHelper.CreateSingleEmail(
                email.From,
                email.To,
                email.Subject,
                email.PlainTextContent,
                email.HtmlContent);

            message.AddAttachment(email.FileName, EncodeAttachment(email.FilePath)); // AddAttachment methodu filename ve base64 codename istiyor. Base64 ü aşağıda encodeAttachment meyhodunda üreteceğiz.

            var client = new SendGridClient(_account.ApiKey);                        // user secret da sakladığımız SendGridAccount objesinin ApiKey property si.

            var emailResponse = await client.SendEmailAsync(message);                // mail gönderip aşağıda da response alıp bunu da bu methodun çağrıldığı yere geri göndereceğiz.

            //return emailResponse; // başlangıçta böyle planladık, ancak NotificationHandler oluşturunca aşağıdaki gibi değişti.

            var serviceResponse = new ServiceResponse
            {
                Message = " email sent...",
                ServiceResponseStatus = Models.Enums.ServiceResponseStatus.Ok
            };

            return(serviceResponse);
        }
Exemple #4
0
        public async Task <CakeOrder> Update(CakeOrder cakeOrder)
        {
            var order = _context.Update(cakeOrder);
            await _context.SaveChangesAsync();

            return(order.Entity);
        }
Exemple #5
0
        public async Task <CakeOrder> Add(CakeOrder cakeOrder)
        {
            var order = _context.CakeOrders.Add(cakeOrder);
            await _context.SaveChangesAsync();

            return(order.Entity);
        }
Exemple #6
0
        public async Task <bool> CreateCakeOrder(CakeOrderViewModel model, CancellationToken ct = default(CancellationToken))
        {
            Customer cus         = _customerRepository.GetByIdentityId(model.IdentityId);
            var      initialData = await this.GetInitialData();

            //calculate total price again
            List <string> toppings = model.Toppings.Split(',').ToList();
            ICake         cake     = initialData.CakeShapes.Where(x => x.Code.Trim() == model.ShapeCode.Trim()).FirstOrDefault();

            cake.Toppings    = initialData.Toppings.Where(w => toppings.Any(code => code.Trim() == w.Code.Trim())).ToList();
            cake.Message     = model.Message;
            cake.Size        = model.Size;
            model.TotalPrice = cake.CalculatePrice();
            CakeOrder order = new CakeOrder();

            order.CustomerId = cus.Id;
            order.Message    = model.Message;
            order.ShapeCode  = model.ShapeCode;
            order.Size       = model.Size;
            order.TotalPrice = model.TotalPrice;

            order = await _cakeOrderRepository.AddAsync(order);

            foreach (var topping in cake.Toppings)
            {
                Topping toppingEntity = new Topping();
                toppingEntity.CakeOrderId = order.Id;
                toppingEntity.Code        = topping.Code;
                await _toppingRepository.AddAsync(toppingEntity);
            }
            return(true);
        }
 public CakeOrderViewModel(CakeOrder cakeOrder)
 {
     Id            = cakeOrder.Id;
     OrderDate     = cakeOrder.OrderDate.ToString("dd/MMM/yyyy", CultureInfo.InvariantCulture);
     CompletedDate = ConvertDateTime(cakeOrder.CompletedDate);
     OrderStatus   = cakeOrder.OrderStatus;
     UserId        = cakeOrder.UserId;
 }
 public CakeViewModel(CakeOrder cake)
 {
     Topping  = cake.Topping;
     Frosting = cake.Frosting;
     Flavour  = cake.Flavour;
     Size     = cake.Size;
     Price    = cake.Price;
 }
        public async Task <CakeOrder> AddCakeOrderAsync(CakeOrder cakeOrder)
        {
            var cakeEntity = await _context
                             .CakeOrders.AddAsync(cakeOrder);

            await _context.SaveChangesAsync();

            return(cakeEntity.Entity);
        }
Exemple #10
0
        public async Task <bool> UpdateAsync(CakeOrder cakeOrder, CancellationToken ct = default(CancellationToken))
        {
            if (!await CakeOrderExists(cakeOrder.Id, ct))
            {
                return(false);
            }
            _context.CakeOrders.Update(cakeOrder);
            await _context.SaveChangesAsync(ct);

            return(true);
        }
 public CakeOrderViewModel(CakeOrder cakeOrder, User user)
 {
     Id            = cakeOrder.Id;
     OrderDate     = cakeOrder.OrderDate.ToString("dd/MMM/yyyy", CultureInfo.InvariantCulture);
     CompletedDate = ConvertDateTime(cakeOrder.CompletedDate);
     OrderStatus   = cakeOrder.OrderStatus;
     UserId        = cakeOrder.UserId;
     UserName      = user.Name;
     PhoneNumber   = user.Number;
     Email         = user.Email;
     Cake          = new CakeViewModel(cakeOrder);
 }
        public async Task <ServiceResponse> SendNotification(CakeOrder cakeOrder)
        {
            var serviceResponse = new ServiceResponse();

            IStatusNotificationRule rule = _notificationRules
                                           .FirstOrDefault(r => r.StatusMatch(cakeOrder.OrderStatus));

            if (rule != null)
            {
                serviceResponse = await rule.Notify(cakeOrder);
            }

            return(serviceResponse);
        }
        public async Task <ServiceResponse> SendMessage(CakeOrder cakeOrder)
        {
            TwilioClient.Init(_account.AccountSid, _account.AuthToken);

            var message = await MessageResource
                          .CreateAsync(from : new PhoneNumber("whatsapp:+14155238886"),
                                       to : new PhoneNumber(cakeOrder.User.Number),
                                       body : $"Your cake order's status code is {cakeOrder.OrderStatus.ToString()}.");

            return(new ServiceResponse
            {
                Message = "WhatsApp message was sent",
                ServiceResponseStatus = ServiceResponseStatus.Ok
            });
        }
Exemple #14
0
        public async Task <ServiceResponse> Notify(CakeOrder order)
        {
            var email         = CreateEmail(order);
            var emailResponse = await _emailService
                                .SendEmailAsync(email)
                                .ConfigureAwait(false);

            var response = new ServiceResponse
            {
                Message = "Invoice has been emailed",
                ServiceResponseStatus = emailResponse.StatusCode == HttpStatusCode.Accepted ? ServiceResponseStatus.Ok : ServiceResponseStatus.FailUnhandled
            };

            return(response);
        }
        public async Task <ServiceResponse> SendMessage(CakeOrder cakeOrder)
        {
            TwilioClient.Init(_twilioAccount.AccountSID, _twilioAccount.AuthToken);

            var message = await MessageResource.CreateAsync(from : new PhoneNumber("whatsapp:+14155238886"), to : new PhoneNumber(cakeOrder.User.Number), body : $"Your cake order status code is {cakeOrder.OrderStatus.ToString()}..."); // burası whatsapp mesajını gönderecek.

            // aşağıda da mesajı gönderdikten sonra ServiceResponse ile mesajın gönderildiğini takip edebileceğiz.

            var response = new ServiceResponse
            {
                Message = "WhatsApp message was sent",
                ServiceResponseStatus = ServiceResponseStatus.Ok
            };

            return(response); // var ı tanımlamadan yukarıda direk return new ServiceResponse {} diye de yapılabilirdi.
        }
Exemple #16
0
        public async Task <ServiceResponse <CakeOrderViewModel> > AddNewOrderAsync(OrderDetails orderDetails)
        {
            var serviceResponse = new ServiceResponse <CakeOrderViewModel>();

            var cakeOrder = new CakeOrder
            {
                Frosting    = orderDetails.Cake.Frosting,
                Flavour     = orderDetails.Cake.Flavour,
                Topping     = orderDetails.Cake.Topping,
                Size        = orderDetails.Cake.Size,
                Price       = orderDetails.Cake.Price,
                OrderDate   = DateTime.UtcNow,
                OrderStatus = OrderStatus.New
            };
            User userEntity;

            if (!orderDetails.IsNewUser)
            {
                userEntity = await _userRepository
                             .GetByPhoneNumber(orderDetails.Number)
                             .ConfigureAwait(false);
            }
            else
            {
                var user = new User
                {
                    Email       = orderDetails.Email,
                    PhoneNumber = orderDetails.Number,
                    Name        = orderDetails.Name
                };
                userEntity = await _userRepository
                             .Add(user)
                             .ConfigureAwait(false);
            }

            cakeOrder.UserId = userEntity.Id;

            var cakeOrderEntity = await _cakeOrderRepository
                                  .Add(cakeOrder)
                                  .ConfigureAwait(false);

            serviceResponse.Content = new CakeOrderViewModel(cakeOrderEntity, userEntity);;
            serviceResponse.ServiceResponseStatus = ServiceResponseStatus.Created;

            return(serviceResponse);
        }
Exemple #17
0
        private Email CreateEmail(CakeOrder order)
        {
            var myEmail     = "*****@*****.**";
            var body        = $"Hello {order.User.Name}, Your cake order has been accepted. Please find your invoice attached";
            var invoicePath = CreateInvoice();
            var email       = new Email
            {
                To               = new EmailAddress(order.User.Email, order.User.Email),
                From             = new EmailAddress(myEmail, "Cloud City Cake Co."),
                Subject          = $"Your invoice for order id {order.Id}",
                HtmlContent      = $"<strong>{body}<strong>",
                PlainTextContent = body,
                Filename         = $"{order.Id}.pdf",
                FilePath         = invoicePath
            };

            return(email);
        }
        public Task <ServiceResponse> Notify(CakeOrder order)
        {
            TwilioClient.Init(_twilioAccount.AccountSid, _twilioAccount.AuthToken);

            var message = MessageResource.Create(
                from: new Twilio.Types.PhoneNumber("whatsapp:+14155238886"),
                body: $"Your  Cake order's status code is {order.OrderStatus.ToString()}",
                to: new Twilio.Types.PhoneNumber($"{order.User.PhoneNumber}")
                );

            var response = new ServiceResponse
            {
                Message = "Whatsapp message sent",
                ServiceResponseStatus = ServiceResponseStatus.Ok
            };

            return(Task.FromResult(response));
        }
Exemple #19
0
        private Email CreateEmail(CakeOrder cakeOrder)
        {
            var emailBody = $"Hello {cakeOrder.User.Name}, your cake order has been accepted. pls find your invoice here";

            var invoicePath = CreateInvoice();

            var email = new Email
            {
                To               = new EmailAddress(cakeOrder.User.Email, cakeOrder.User.Name),
                From             = new EmailAddress(_account.EmailFromAddress, "PastannecimCo"), // userSecretsdaki kendi email adresimiz
                Subject          = $"Your invoice for invoice id {cakeOrder.Id}",
                HtmlContent      = $"<strong>{emailBody}<strong>",
                PlainTextContent = emailBody,
                FileName         = $"{cakeOrder.User.Name} {cakeOrder.Id}.pdf",
                FilePath         = invoicePath
            };

            return(email);
        }
Exemple #20
0
        private Email CreateEmail(CakeOrder cakeOrder)
        {
            var body =
                $"Hello {cakeOrder.User.Name}, Your cake order has been accepted. Please find your invoice attached.";
            var invoicePath = CreateInvoice();

            var email = new Email
            {
                To               = new EmailAddress(cakeOrder.User.Email, cakeOrder.User.Name),
                From             = new EmailAddress(_account.EmailFromAddress, "The Cloud City Cake Co."),
                Subject          = $"Your invoice for order id {cakeOrder.Id}",
                HtmlContent      = $"<strong>{body}<strong>",
                PlainTextContent = body,
                Filename         = $"{cakeOrder.Id}.pdf",
                FilePath         = invoicePath
            };

            return(email);
        }
Exemple #21
0
        public async Task <ServiceResponse> SendNotification(CakeOrder cakeOrder)
        {
            var serviceResponse = new ServiceResponse();
            // burada bir swittch ile ne tür notification göndereceğimize karar verebilirdik. Ama bunu yerine ruleset tanımlayıp kullandık.


            //yukarıda ctor da mevcut bütün notificationrule ları liste olarak aldık. Şimdi onların içinden bizim ckaeOrder'ın status una eşit olanı bulmamız lazım.

            IStatusNotificationRule rule = _notificationRules.FirstOrDefault(r => r.StatusMatch(cakeOrder.OrderStatus));

            if (rule != null)
            {
                serviceResponse = await rule.Notify(cakeOrder); // kuralı bulduysak burada kuralın notificationunun göndereceğiz.
            }



            return(serviceResponse);
        }
Exemple #22
0
        public async Task <ServiceResponse <CakeOrderViewModel> > AddNewOrderAsync(OrderDetails orderDetails)
        {
            var serviceResponse = new ServiceResponse <CakeOrderViewModel>();

            var user = await _userRepository.GetUserByPhoneNumberAsync(orderDetails.Number);

            if (user == null)
            {
                user = new User
                {
                    Name   = orderDetails.Name,
                    Number = orderDetails.Number,
                    Email  = orderDetails.Email
                };

                user = await _userRepository.AddUserAsync(user);
            }

            var cakeOrder = new CakeOrder
            {
                Flavour     = orderDetails.Cake.Flavour,
                Frosting    = orderDetails.Cake.Frosting,
                Topping     = orderDetails.Cake.Topping,
                Size        = orderDetails.Cake.Size,
                Price       = orderDetails.Cake.Price,
                OrderDate   = DateTime.UtcNow,
                UserId      = user.Id,
                OrderStatus = OrderStatus.New
            };


            var cakeOrderEntity = await _cakeOrderRepository
                                  .AddCakeOrderAsync(cakeOrder)
                                  .ConfigureAwait(false);

            serviceResponse.Content = new CakeOrderViewModel(cakeOrderEntity, user);
            serviceResponse.ServiceResponseStatus = ServiceResponseStatus.Created;

            return(serviceResponse);
        }
Exemple #23
0
        public async Task <ServiceResponse> SendEmail(CakeOrder cakeOrder)
        {
            var email   = CreateEmail(cakeOrder);
            var message = MailHelper.CreateSingleEmail(
                email.From,
                email.To,
                email.Subject,
                email.PlainTextContent,
                email.HtmlContent);

            message.AddAttachment(email.Filename, EncodedAttachment(email.FilePath));

            var client = new SendGridClient(_account.ApiKey);

            var _ = await client.SendEmailAsync(message);

            return(new ServiceResponse
            {
                Message = "email sent",
                ServiceResponseStatus = ServiceResponseStatus.Ok
            });
        }
 public async Task <ServiceResponse> Notify(CakeOrder cakeOrder)
 {
     return(await _messagingService.SendMessage(cakeOrder));
 }
Exemple #25
0
 public async Task DeleteOrder(CakeOrder cakeOrder)
 {
     _context.CakeOrders.Remove(cakeOrder);
     await _context.SaveChangesAsync();
 }
Exemple #26
0
 public async Task <ServiceResponse> Notify(CakeOrder cakeOrder)
 {
     return(await _emailService.SendEmail(cakeOrder));
 }