示例#1
0
        public async Task <OperationStatusInfo> AddSales(object addedSale)
        {
            return(await Task.Run(() =>
            {
                Dictionary <Type, object> collection = new Dictionary <Type, object>();
                OperationStatusInfo operationStatusInfo = new OperationStatusInfo(operationStatus: OperationStatus.Done);

                string attachedSaleObjectText = addedSale.ToString();
                Sale newSale = JsonConvert.DeserializeObject <Sale>(attachedSaleObjectText);
                AddSaleRequest addSaleRequest = new AddSaleRequest(newSale.Product, newSale.Count.ToString(),
                                                                   newSale.Datetime.ToString(), newSale.Price.ToString(), newSale.Seller);

                try
                {
                    AddSaleResponse addSaleResponse = hubEnvironment.UseCaseFactory
                                                      .Create <IUseCase <AddSaleRequest, AddSaleResponse> >()
                                                      .Execute(addSaleRequest);
                    collection.Add(typeof(List <Sale>), addSaleResponse.Sales);
                    collection.Add(typeof(List <Product>), addSaleResponse.Products);
                    operationStatusInfo.AttachedObject = collection;
                    operationStatusInfo.AttachedInfo = "Sale is added!";
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    operationStatusInfo.OperationStatus = OperationStatus.Cancelled;
                    operationStatusInfo.AttachedInfo = ex.Message;
                }

                return operationStatusInfo;
            }));
        }
示例#2
0
        public void AddSaleUseCaseEqualTest()
        {
            price = count * product.SalesPrice;
            AddSaleRequest addSaleRequest = new AddSaleRequest(product, count.ToString(), date.ToString(), price.ToString(), seller);

            IRepositoryFactory repositoryFactory = new RepositoryFactory(new DBContext());
            IActivityFactory   activityFactory   = new ActivityFactory(repositoryFactory, new ValidationRuleFactory());
            IUseCaseFactory    useCaseFactory    = new UseCaseFactory(activityFactory);
            AddSaleResponse    addSaleResponse   = useCaseFactory.Create <IUseCase <AddSaleRequest, AddSaleResponse> >().Execute(addSaleRequest);

            Assert.AreEqual(addSaleResponse.Sales[1].Product.Id, product.Id);
            Assert.AreEqual(addSaleResponse.Sales[1].Count, count);
            Assert.AreEqual(addSaleResponse.Sales[1].Datetime.Ticks, date.Ticks);
            Assert.AreEqual(addSaleResponse.Sales[1].Price, price);
            Assert.AreEqual(addSaleResponse.Sales[1].Seller.Id, seller.Id);
        }
示例#3
0
        public void AddSaleUseCaseNotEqualTest()
        {
            Exception exception = null;

            try
            {
                IRepositoryFactory repositoryFactory = new RepositoryFactory(new DBContext());
                IActivityFactory   activityFactory   = new ActivityFactory(repositoryFactory, new ValidationRuleFactory());
                IUseCaseFactory    useCaseFactory    = new UseCaseFactory(activityFactory);
                AddSaleRequest     addSaleRequest    = new AddSaleRequest(product, "asd", date.ToString(), price.ToString(), seller);
                AddSaleResponse    addSaleResponse   = useCaseFactory.Create <IUseCase <AddSaleRequest, AddSaleResponse> >().Execute(addSaleRequest);
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            Assert.AreEqual(exception.Message, "Count must have digits only");
        }
示例#4
0
        public override async Task <ErrorCodeReply> AddSale(AddSaleRequest request, ServerCallContext context)
        {
            // db updated successfully?
            bool dbUpdateSuccess = false;

            MySqlConnection db = new AppDb().Connection;
            await db.OpenAsync();

            var cmd = db.CreateCommand();
            MySqlTransaction myTrans = await db.BeginTransactionAsync();

            cmd.Transaction = myTrans;

            // Create a new sale
            cmd.CommandText = $"insert into sale (total_billed, date) Values (0, NOW()); SELECT LAST_INSERT_ID();";
            var reader = await cmd.ExecuteReaderAsync();

            await reader.ReadAsync();

            // get this saleid - used later
            int saleid = reader.GetFieldValue <int>(0);
            await reader.CloseAsync();

            float totalBilled = 0f;

            // Loop to add items to ItemDetail table
            foreach (var itemDetail in request.ItemDetails)
            {
                // Find item
                cmd.CommandText = $"SELECT item_id, price FROM item WHERE name=\"{itemDetail.ItemName}\"";
                reader          = await cmd.ExecuteReaderAsync();

                await reader.ReadAsync();

                int itemid = reader.GetFieldValue <int>(0);
                totalBilled += reader.GetFieldValue <float>(1) * itemDetail.Quantity;

                await reader.CloseAsync();

                // insert saleid into ItemDetail
                cmd.CommandText = $"insert into itemdetail (item_id, quantity, sale_id) Values ({itemid}, {itemDetail.Quantity}, {saleid})";

                await cmd.ExecuteNonQueryAsync();
            }

            // update saleid to proper
            cmd.CommandText = $"UPDATE sale SET total_billed = {totalBilled} WHERE sale_id = {saleid}";
            reader          = await cmd.ExecuteReaderAsync();

            await reader.ReadAsync();

            await reader.CloseAsync();

            await myTrans.CommitAsync();

            await db.CloseAsync();

            _logger.LogInformation($"Sale with id {saleid}, successfully added to the database");

            return(await Task.FromResult(new ErrorCodeReply
            {
                ErrorCode = dbUpdateSuccess
            }));
        }