Beispiel #1
0
        public async Task <ResponseApiHelper> AddPurchaseAsync(List <ProductDto> productsDto)
        {
            var purchases         = FormatPurchase(productsDto);
            var month             = DateTime.Today.Month.ToString().Length == 1 ? $"0{DateTime.Today.Month}" : DateTime.Today.Month.ToString();
            var key               = $"{month}-{DateTime.Today.Year}";
            var purchasesFirebase = await _purchaseRepository.GetPurchases(key);

            purchasesFirebase = purchasesFirebase.Union(purchases).ToList();
            await _purchaseRepository.AddAsync(purchasesFirebase);

            return(new ResponseApiHelper {
                Message = "Compra cadastrada", Success = true, Data = purchases
            });
        }
Beispiel #2
0
        public async Task PurchaseMovie(PurchaseRequestModel purchaseRequest)
        {
            //Validate Authorization
            // if(_currentUserService.UserId!=purchaseRequest.UserId)
            //     throw new Exception("You are not authorized to purchase the movie!");
            //Check whether the movie is purchased
            if (await IsMoviePurchased(purchaseRequest))
            {
                throw new Exception("The movie has been purchased");
            }
            var movie = await _movieService.GetMovieAsync(purchaseRequest.MovieId);

            purchaseRequest.TotalPrice       = movie.Price;
            purchaseRequest.PurchaseDateTime = DateTime.Now;
            purchaseRequest.PurchaseNumber   = Guid.NewGuid();
            Purchase purchase = new Purchase
            {
                UserId           = purchaseRequest.UserId,
                MovieId          = purchaseRequest.MovieId,
                PurchaseNumber   = purchaseRequest.PurchaseNumber,
                PurchaseDateTime = purchaseRequest.PurchaseDateTime,
                TotalPrice       = purchaseRequest.TotalPrice
            };

            await _purchaseRepository.AddAsync(purchase);
        }
Beispiel #3
0
        public async Task <PurchaseViewModel> Add(AddPurchaseViewModel addPurchaseViewModel)
        {
            var currentDealer = await _dealerApplication.GetByCpf(addPurchaseViewModel.Cpf);

            if (currentDealer == null)
            {
                await _mediator.PublishEvent(new ApplicationNotification($"Não foi encontrado nenhum revendedor com o cpf: {addPurchaseViewModel.Cpf}"));

                return(null);
            }

            var purchase = currentDealer.IsAproved() ? new Purchase(currentDealer.Id, addPurchaseViewModel.Code, addPurchaseViewModel.Value, "Aprovado", addPurchaseViewModel.Date)
                                              : new Purchase(currentDealer.Id, addPurchaseViewModel.Code, addPurchaseViewModel.Value, "Em validação", addPurchaseViewModel.Date);

            if (!purchase.IsValid())
            {
                foreach (var error in purchase.ValidationResult.Errors)
                {
                    await _mediator.PublishEvent(new ApplicationNotification(error.ErrorMessage));
                }

                return(null);
            }

            await _purchaseRepository.AddAsync(purchase);

            await base.Commit();

            return(_mapper.Map <PurchaseViewModel>(purchase));
        }
        public async Task <Purchase> CreateAsync(Purchase newData)
        {
            if (newData == null)
            {
                throw new ArgumentNullException(nameof(newData));
            }

            await _purchaseRepository.AddAsync(newData);

            return(newData);
        }
        public async Task <PurchaseResponse> AddAsync(Purchase purchase)
        {
            try
            {
                await _purchaseRepository.AddAsync(purchase);

                await _unitOfWork.CompleteAsync();

                return(new PurchaseResponse(purchase));
            } catch (Exception e)
            {
                return(new PurchaseResponse("An error ocurred while saving the purchase:\n" + e));
            }
        }
Beispiel #6
0
        public async Task PurchaseMovie(PurchaseRequestModel purchaseRequest)
        {
            var purchased = new Purchase
            {
                UserId           = purchaseRequest.userId,
                PurchaseNumber   = purchaseRequest.purchaseNumber,
                TotalPrice       = (decimal)purchaseRequest.totalPrice,
                PurchaseDateTime = purchaseRequest.purchaseDateTime,
                MovieId          = purchaseRequest.movieId
            };

            var createdPurchased = await _purchaseRepository.AddAsync(purchased);

            Console.WriteLine("Success add purchased movies");
        }
Beispiel #7
0
        public async Task <bool> PurchaseMovie(PurchaseRequestModel purchaseRequestModel)
        {
            var purchase = new Purchase
            {
                MovieId          = purchaseRequestModel.MovieId,
                UserId           = purchaseRequestModel.UserId,
                TotalPrice       = purchaseRequestModel.Price,
                PurchaseDateTime = DateTime.Now,
                PurchaseNumber   = Guid.NewGuid()
            };

            var createdPurchase = await _purchaseRepository.AddAsync(purchase);

            return(createdPurchase != null);
        }
Beispiel #8
0
        public async Task <Purchase> PurchaseMovie(PurchaseRequestModel purchaseRequestModel)
        {
            var movie = await _movieService.GetMovieById(purchaseRequestModel.MovieId);

            var p = new Purchase
            {
                UserId           = purchaseRequestModel.UserId,
                PurchaseNumber   = purchaseRequestModel.PurchaseNumber.Value,
                TotalPrice       = movie.Price.Value,
                MovieId          = purchaseRequestModel.MovieId,
                PurchaseDateTime = purchaseRequestModel.PurchaseDate.Value
            };

            return(await _purchaseRepository.AddAsync(p));
        }
Beispiel #9
0
        public async Task <PurchaseResponse> SaveAsync(Purchase purchase)
        {
            try
            {
                await _purchaseRepository.AddAsync(purchase);

                await _unitOfWork.CompleteAsync();

                return(new PurchaseResponse(purchase));
            }
            catch (Exception ex)
            {
                return(new PurchaseResponse($"An error ocurred when saving the purchase : {ex.Message}"));
            }
        }
        public async Task Insert(PurchaseModel model)
        {
            var loggedUser = await _authService.GetLoggedUser();

            if (!model.Products.Any())
            {
                throw new Exception("A compra está vazia");
            }

            var purchase = await CreateNewPurchase(model, loggedUser);

            await _purchaseRepository.AddAsync(purchase);

            await _unitOfWork.CommitAsync();
        }
        public async Task <ApplicationDataResult <PurchaseDto> > AddAsync(AddOrUpdatePurchaseDto data)
        {
            ValidationResult validationResult = await _validatorPurchase.ValidateAsync(data);

            if (validationResult.IsValid is false)
            {
                return(FactoryFromValidationResult <PurchaseDto>(validationResult));
            }

            Purchase purchase = _mapper.Map <Purchase>(data);

            await _purchaseRepository.AddAsync(purchase);

            await _purchaseRepository.UnitOfWork.CommitAsync();

            return(FactoryResultFromData(_mapper.Map <PurchaseDto>(purchase)));
        }
Beispiel #12
0
        public async Task PurchaseMovie(PurchaseRequestModel purchaseRequest)
        {
            var movie = await _purchaseRepository.GetByIdAsync(purchaseRequest.MovieId);

            //var user = await _purchaseRepository.GetByIdAsync(purchaseRequest.UserId);
            purchaseRequest.TotalPrice       = movie.TotalPrice;
            purchaseRequest.PurchaseNumber   = movie.PurchaseNumber;
            purchaseRequest.PurchaseDateTime = movie.PurchaseDateTime;
            var purchase = new Purchase
            {
                UserId           = purchaseRequest.UserId,
                MovieId          = purchaseRequest.MovieId,
                TotalPrice       = purchaseRequest.TotalPrice,
                PurchaseNumber   = purchaseRequest.PurchaseNumber,
                PurchaseDateTime = purchaseRequest.PurchaseDateTime
            };
            await _purchaseRepository.AddAsync(purchase);
        }
Beispiel #13
0
        public async Task PurchaseMovie(PurchaseRequestModel purchaseRequest)
        {
            if (await IsMoviePurchased(purchaseRequest))
            {
                throw new Exception("Movie already Purchased");
            }
            // Get Movie Price from Movie Table
            var movie = await _movieService.GetMovieAsync(purchaseRequest.MovieId);

            purchaseRequest.TotalPrice = movie.Price;

            var purchase = new Purchase
            {
                UserId  = purchaseRequest.UserId,
                MovieId = purchaseRequest.MovieId
            };
            await _purchaseRepository.AddAsync(purchase);
        }
Beispiel #14
0
        public async Task PurchaseMovie(PurchaseRequestModel purchaseRequest)
        {
            if (await IsMoviePurchased(purchaseRequest))
            {
                throw new HttpRequestException("Movie Already Purchased");
            }

            var movie = await _movieService.GetMovieAsync(purchaseRequest.MovieId);

            purchaseRequest.TotalPrice = movie.Price;
            var purchase = new Purchase()
            {
                MovieId          = purchaseRequest.MovieId,
                UserId           = purchaseRequest.UserId,
                PurchaseDateTime = purchaseRequest.PurchaseDateTime,
                TotalPrice       = (decimal)purchaseRequest.TotalPrice,
                PurchaseNumber   = purchaseRequest.PurchaseNumber
            };
            await _purchaseRepository.AddAsync(purchase);
        }
Beispiel #15
0
        public async Task PurchaseMovie(PurchaseRequestModel purchaseRequest)
        {
            if (_currentUserService.UserId != purchaseRequest.UserId)
            {
                throw new HttpException(HttpStatusCode.Unauthorized, "You are not authorized to purchase.");
            }

            if (await IsMoviePurchased(purchaseRequest))
            {
                throw new ConflictException("Movie has already purchased.");
            }

            var movie = await _movieService.GetMovieById(purchaseRequest.MovieId);

            purchaseRequest.TotalPrice       = movie.Price;
            purchaseRequest.PurchaseDateTime = DateTime.Now;
            purchaseRequest.PurchaseNumber   = Guid.NewGuid();

            var purchase = _mapper.Map <Purchase>(purchaseRequest);
            await _purchaseRepository.AddAsync(purchase);
        }
Beispiel #16
0
        public async Task <PurchaseResponseModel> PurchaseMovie(PurchaseRequestModel purchaseRequestModel)
        {
            var purchase = new Purchase
            {
                MovieId          = purchaseRequestModel.MovieId,
                UserId           = purchaseRequestModel.UserId,
                PurchaseNumber   = purchaseRequestModel.PurchaseNumber,
                PurchaseDateTime = purchaseRequestModel.PurchaseTime,
            };
            var reponse = await _purchaseRepository.AddAsync(purchase);

            var purchaseResponse = new PurchaseResponseModel
            {
                MovieId        = reponse.MovieId,
                UserId         = reponse.UserId,
                PurchaseNumber = reponse.PurchaseNumber,
                PurchaseTime   = reponse.PurchaseDateTime,
                Price          = reponse.TotalPrice,
            };

            return(purchaseResponse);
        }
        public async Task <Purchase> PlaceOrder(UserPurchaseRequestModel userPurchaseRequestModel)
        {
            var movie = await _movieService.GetMovieById(userPurchaseRequestModel.MovieId);

            if (await IsMoviePurchased(userPurchaseRequestModel.UserId, userPurchaseRequestModel.MovieId) == true)
            {
                throw new Exception("You already bought this movie!");
            }
            if (await IsMoviePurchased(userPurchaseRequestModel.UserId, userPurchaseRequestModel.MovieId) == false)
            {
                var purchase = new Purchase()
                {
                    UserId           = userPurchaseRequestModel.UserId,
                    PurchaseNumber   = userPurchaseRequestModel.PurchaseNumber,
                    TotalPrice       = movie.Price.Value,
                    PurchaseDateTime = userPurchaseRequestModel.PurchaseDateTime,
                    MovieId          = userPurchaseRequestModel.MovieId
                };

                var createPurchase = await _purchaseRepository.AddAsync(purchase);


                //var response = new UserPurchaseResponseModel()
                //{
                //    Id = createPurchase.Id,
                //    UserId = createPurchase.UserId,
                //    PurchaseNumber = createPurchase.PurchaseNumber,
                //    TotalPrice = createPurchase.TotalPrice,
                //    PurchaseDateTime = createPurchase.PurchaseDateTime,
                //    MovieId = createPurchase.MovieId,
                //    movie = movie,
                //    IsMoviePurchased = true


                //};
                return(createPurchase);
            }
            return(null);
        }
        public async Task <bool> PurchaseMovie(PurchaseRequestModel purchaseRequest)
        {
            decimal?totalPrice = await _purchaseRepository.GetPriceByMovieId(purchaseRequest.MovieId);

            DateTime PurchaseDateTime = DateTime.Now;
            Guid     PurchaseNumber   = Guid.NewGuid();


            var purchaseResponse = new Purchase
            {
                MovieId          = purchaseRequest.MovieId,
                PurchaseDateTime = PurchaseDateTime,
                PurchaseNumber   = PurchaseNumber,
                TotalPrice       = (decimal)totalPrice,
                UserId           = purchaseRequest.UserId,
            };
            var createdPurchase = await _purchaseRepository.AddAsync(purchaseResponse);

            if (createdPurchase != null && createdPurchase.Id > 0)
            {
                return(true);
            }
            return(false);
        }
Beispiel #19
0
        public async Task <Purchase> AddAsync(int userId, PurchaseRequest request)
        {
            var paymentMethod = await _purchaseRepository.FindPaymentMethodByIdAsync(request.PaymentMethodId);

            if (paymentMethod == null)
            {
                throw new ApiException("Payment method was not found!");
            }

            var purchase = _mapper.Map <Purchase>(request);

            purchase.PaymentMethod = paymentMethod;
            purchase.UserId        = userId;
            purchase.StoreId       = request.StoreId;
            var list = new List <Product>();

            var updatedProducts = new List <Product>();

            foreach (var requestReceiptPosition in request.ReceiptPositions)
            {
                var product = await _productRepository.Find(request.StoreId, requestReceiptPosition.ProductId);

                if (product == null)
                {
                    throw new ApiException($"Product with id {requestReceiptPosition.ProductId} was not found!");
                }
                if (product.CountInStock < requestReceiptPosition.Count)
                {
                    throw new ApiException($"{product.CountInStock} items left" +
                                           $" for product with id {product.ProductId}" +
                                           $". It is impossible to take {requestReceiptPosition.Count}");
                }

                list.Add(product);
                product.CountInStock -= requestReceiptPosition.Count;
                updatedProducts.Add(product);
            }

            purchase.ReceiptPositions = list.Select((x, i) => new ProductReceiptInformation
            {
                Product          = x,
                Count            = request.ReceiptPositions[i].Count,
                CustomCategories = request.ReceiptPositions[i].CustomCategories?.Select(async name =>
                {
                    var customCategory = await _purchaseRepository.FindCustomCategoryASync(userId, name);
                    return(new CustomCategoryForProduct
                    {
                        CustomCategory = customCategory ?? new CustomCategory(name)
                    });
                }).Select(t => t.Result).ToList() ?? new List <CustomCategoryForProduct>()
            }).ToList();


            var addedPurchase = await _purchaseRepository.AddAsync(purchase);

            await _purchaseRepository.SaveChangesAsync();

            updatedProducts.ForEach(x => _productRepository.Update(x));
            await _productRepository.SaveChangesAsync();

            return(addedPurchase);
        }
Beispiel #20
0
        public async Task <Purchase> PurchaseMovie(UserPurchaseRequestModel requestModel)
        {
            var purchase = new Purchase
            {
                UserId           = requestModel.UserId,
                MovieId          = requestModel.MovieId,
                PurchaseNumber   = requestModel.PurchaseNumber,
                TotalPrice       = requestModel.Price,
                PurchaseDateTime = requestModel.PurchaseTime
            };

            return(await _purchaseRepository.GetExistsAsync(p =>
                                                            p.UserId == purchase.UserId && p.MovieId == purchase.MovieId) ? null : await _purchaseRepository.AddAsync(purchase));
        }
Beispiel #21
0
 public async Task CreateAsync(Purchase purchase)
 => await _purchaseRepository.AddAsync(purchase);