Beispiel #1
0
        public IActionResult AddOrder([FromBody] OrderDto orderDto)
        {
            try
            {
                Customer customer = _unitOfWork.Customers.Get(orderDto.CustomerId);

                if (customer == null)
                {
                    return(NotFound("Customer Not Found"));
                }

                Result <InvoiceNo>   invoiceNoOrError   = InvoiceNo.Create(orderDto.InvoiceNo);
                Result <InvoiceDate> invoiceDateOrError = InvoiceDate.Create(orderDto.InvoiceDate);
                Result <ReferenceNo> referenceNoOrError = ReferenceNo.Create(orderDto.ReferenceNo);

                Result result = Result.Combine(invoiceNoOrError, invoiceDateOrError, referenceNoOrError);
                if (result.IsFailure)
                {
                    return(BadRequest(result.Error));
                }

                var order = new Order(invoiceNoOrError.Value, invoiceDateOrError.Value, referenceNoOrError.Value, orderDto.Note, customer);

                _unitOfWork.Orders.Add(order);
                _unitOfWork.Complete();

                return(Ok());
            }
            catch (Exception e)
            {
                return(StatusCode(500, e.Message));
            }
        }
Beispiel #2
0
        public IActionResult PutOrder(long id, OrderDto orderDto)
        {
            try
            {
                if (id != orderDto.Id)
                {
                    return(BadRequest());
                }

                Order order = _unitOfWork.Orders.Get(id);
                if (order == null)
                {
                    return(NotFound("Order Not Found"));
                }

                Customer customer = _unitOfWork.Customers.Get(orderDto.CustomerId);

                if (customer == null)
                {
                    return(NotFound("Customer Not Found"));
                }

                Result <InvoiceDate> invoiceDateOrError = InvoiceDate.Create(orderDto.InvoiceDate);
                Result <ReferenceNo> referenceNoOrError = ReferenceNo.Create(orderDto.ReferenceNo);

                Result result = Result.Combine(invoiceDateOrError, referenceNoOrError);
                if (result.IsFailure)
                {
                    return(BadRequest(result.Error));
                }

                order.UpdateOrder(invoiceDateOrError.Value, referenceNoOrError.Value, orderDto.Note, customer);

                _unitOfWork.Complete();

                return(Ok());
            }
            catch (Exception e)
            {
                return(StatusCode(500, new { error = e.Message }));
            }
        }
        public void Configure(EntityTypeBuilder <Order> orderConfiguration)
        {
            orderConfiguration.Property(b => b.Id).HasColumnName("OrderID");

            orderConfiguration.Property(b => b.InvoiceNo).HasConversion(p => p.Value, p => InvoiceNo.Create(p).Value).IsRequired().HasMaxLength(10);
            orderConfiguration.Property(b => b.InvoiceDate).HasConversion(p => p.Value, p => InvoiceDate.Create(p).Value).IsRequired();
            orderConfiguration.Property(b => b.ReferenceNo).HasConversion(p => p.Value, p => ReferenceNo.Create(p).Value).IsRequired().HasMaxLength(10);

            orderConfiguration.HasOne(s => s.Customer).WithMany().IsRequired();
            orderConfiguration.HasMany(s => s.OrderLines).WithOne(e => e.Order);
        }