示例#1
0
        public void Test_NotEqual_SaleInputMessage_AllArgs_Constructor()
        {
            var sale = new Sale();

            sale.Id        = 1;
            sale.ProductId = 2;
            sale.Quantity  = 20;
            sale.CreatedAt = new DateTime(2020, 08, 23);
            sale.UpdatedAt = new DateTime(2020, 10, 15);
            var messageType = MessageType.ProductCreated;
            var updatedSale = new UpdatedSale(1, 2, 10, 20);
            var test        = new SaleInputMessage(messageType, sale);

            var sale1 = new Sale();

            sale1.Id        = 1;
            sale1.ProductId = 2;
            sale1.Quantity  = 20;
            sale1.CreatedAt = new DateTime(2020, 08, 23);
            sale1.UpdatedAt = new DateTime(2020, 10, 15);
            var messageType1 = MessageType.ProductCreated;
            var updatedSale1 = new UpdatedSale(1, 2, 10, 20);
            var test1        = new SaleInputMessage(messageType1, sale1);

            Assert.NotEqual(test.GetHashCode(), test1.GetHashCode());
            Assert.NotEqual(test, test1);
            Assert.NotEqual(updatedSale, updatedSale1);
        }
示例#2
0
        public void Test_SaleOutputMessage_Getter()
        {
            var sale = new Sale();

            sale.Id        = 1;
            sale.ProductId = 2;
            sale.Total     = 100M;
            sale.Quantity  = 20;
            sale.CreatedAt = new DateTime(2020, 08, 23);
            sale.UpdatedAt = new DateTime(2020, 10, 15);
            var messageType = MessageType.SaleCreated;
            var updatedSale = new UpdatedSale(1, 2, 10, 20);
            var test        = new SaleOutputMessage(messageType, sale, updatedSale);

            Assert.Equal(20, test.Quantity);
            Assert.Equal(2, test.ProductId);
            Assert.Equal(1, test.SaleId);
            Assert.Equal(new DateTime(2020, 08, 23), test.CreatedAt);
            Assert.Equal(new DateTime(2020, 10, 15), test.UpdatedAt);
            Assert.Equal(1, test.UpdatedSale.OldProductId);
            Assert.Equal(2, test.UpdatedSale.NewProductId);
            Assert.Equal(10, test.UpdatedSale.OldQuantity);
            Assert.Equal(20, test.UpdatedSale.NewQuantity);
            Assert.Equal("SaleCreated", test.MessageTitle);
        }
示例#3
0
        public void Test_Updated_Sale_Getter()
        {
            var sale1 = new UpdatedSale(1, 2, 10, 20);

            Assert.Equal(1, sale1.OldProductId);
            Assert.Equal(2, sale1.NewProductId);
            Assert.Equal(10, sale1.OldQuantity);
            Assert.Equal(20, sale1.NewQuantity);
        }
示例#4
0
        public void Test_UpdatedSale_AllArgs_Constructor(int oldProductId, int newProductId, int oldQuantity, int newQuantity)
        {
            var test = new UpdatedSale(oldProductId, newProductId, oldQuantity, newQuantity);

            Assert.Equal(oldProductId, test.OldProductId);
            Assert.Equal(newProductId, test.NewProductId);
            Assert.Equal(oldQuantity, test.OldQuantity);
            Assert.Equal(newQuantity, test.NewQuantity);
        }
示例#5
0
 public SaleOutputMessage(MessageType messageType, Sale sale, UpdatedSale updatedSale = null)
 {
     MessageTitle = messageType.ToString();
     SaleId       = sale.Id;
     ProductId    = sale.ProductId;
     Quantity     = sale.Quantity;
     CreatedAt    = sale.CreatedAt;
     UpdatedAt    = sale.UpdatedAt;
     UpdatedSale  = updatedSale;
 }
示例#6
0
        public void Test_NotEqual_Updated_Sale_AllArgs_Constructor()
        {
            var sale1 = new UpdatedSale(1, 2, 10, 20);
            var sale2 = new UpdatedSale(1, 2, 10, 20);

            Assert.NotEqual(sale1.GetHashCode(), sale2.GetHashCode());
            Assert.Equal(1, sale1.OldProductId);
            Assert.Equal(2, sale1.NewProductId);
            Assert.Equal(10, sale2.OldQuantity);
            Assert.Equal(20, sale2.NewQuantity);
        }
示例#7
0
        public void Test_UpdatedSale_Getter_Setter_Warehouse()
        {
            var test1 = new UpdatedSale(1, 2, 10, 20);

            test1.NewQuantity  = 30;
            test1.OldQuantity  = 5;
            test1.OldProductId = 4;
            test1.NewProductId = 5;

            Assert.Equal(30, test1.NewQuantity);
            Assert.Equal(5, test1.OldQuantity);
            Assert.Equal(4, test1.OldProductId);
            Assert.Equal(5, test1.NewProductId);
        }
示例#8
0
        public Task SendMessageAsync(MessageType messageType, Sale sale, UpdatedSale updatedSale = null)
        {
            _messageConnectionString = Configuration["MessageConnectionString"];
            var messageToSend = _factory.Create(messageType, sale, updatedSale);

            var serviceBusClient = new TopicClient(_messageConnectionString, "sale-send");

            var message = new Message(messageToSend.ToJsonBytes());

            message.ContentType = "application/json";
            message.UserProperties.Add("CorrelationId", Guid.NewGuid().ToString());

            serviceBusClient.SendAsync(message);

            return(Task.CompletedTask);
        }
示例#9
0
        public new async Task <object> Delete(int id)
        {
            if (id <= 0 || _repository.GetById(id) == null)
            {
                return(null);
            }
            var saleToDelete = _repository.GetById(id);
            var updatedSale  = new UpdatedSale(
                saleToDelete.ProductId,
                saleToDelete.ProductId,
                saleToDelete.Quantity,
                0);

            _repository.Delete(id);
            await _messageHandler.SendMessageAsync(MessageType.SaleCancelled, saleToDelete, updatedSale);

            _updateProduct.CancelSale(saleToDelete);

            return(new { Message = "Venda cancelada com sucesso." });
        }
示例#10
0
        public new async Task <object> Update(int id, Sale sale)
        {
            if (id <= 0 || _repository.GetById(id) == null)
            {
                return(null);
            }

            var product      = _productRepository.GetById(sale.ProductId);
            var saleToUpdate = _repository.GetById(id);
            var updatedSale  = new UpdatedSale(
                saleToUpdate.ProductId,
                sale.ProductId,
                saleToUpdate.Quantity,
                sale.Quantity);

            sale.CreatedAt = saleToUpdate.CreatedAt;
            sale.UpdatedAt = DateTime.Now;
            sale.Total     = product.Price * sale.Quantity;
            sale.Id        = id;

            if (updatedSale.NewProductId != updatedSale.OldProductId)
            {
                return new
                       {
                           Message = "Não é possível alterar " +
                                     "o produto vendido. É preciso cancelar a venda e criar uma nova venda."
                       }
            }
            ;

            _repository.Update(id, sale);
            await _messageHandler.SendMessageAsync(MessageType.SaleUpdated, sale, updatedSale);

            _updateProduct.UpdateStock(sale, saleToUpdate);

            return(new { Message = "Venda alterada com sucesso." });
        }
示例#11
0
        public void Test_SaleInputMessage_Setter()
        {
            var sale = new Sale();

            sale.Id        = 1;
            sale.ProductId = 2;
            sale.Quantity  = 20;
            sale.CreatedAt = new DateTime(2020, 08, 23);
            sale.UpdatedAt = new DateTime(2020, 10, 15);
            var updatedSale = new UpdatedSale(1, 2, 10, 20);
            var messageType = MessageType.ProductCreated;
            var test        = new SaleInputMessage(messageType, sale);

            test.MessageTitle             = "SaleCreated";
            test.SaleId                   = 2;
            test.ProductId                = 1;
            test.Quantity                 = 30;
            test.CreatedAt                = DateTime.Today;
            test.UpdatedAt                = DateTime.Today;
            test.UpdatedSale              = updatedSale;
            test.UpdatedSale.OldProductId = 4;
            test.UpdatedSale.NewProductId = 3;
            test.UpdatedSale.OldQuantity  = 20;
            test.UpdatedSale.NewQuantity  = 50;

            Assert.Equal(30, test.Quantity);
            Assert.Equal(1, test.ProductId);
            Assert.Equal(2, test.SaleId);
            Assert.Equal(DateTime.Today, test.CreatedAt);
            Assert.Equal(DateTime.Today, test.UpdatedAt);
            Assert.Equal("SaleCreated", test.MessageTitle);
            Assert.Equal(4, test.UpdatedSale.OldProductId);
            Assert.Equal(3, test.UpdatedSale.NewProductId);
            Assert.Equal(20, test.UpdatedSale.OldQuantity);
            Assert.Equal(50, test.UpdatedSale.NewQuantity);
        }
示例#12
0
 public SaleOutputMessage Create(MessageType messageType, Sale sale, UpdatedSale updatedSale = null)
 {
     return(new SaleOutputMessage(messageType, sale, updatedSale));
 }