public void MapperTest_ToDataObjects() { Client client = new Client("Test name", "Test addres"); DClient dClient = Mapper.ToDClient(client); dClient.Id.Should().Be(client.Id); dClient.Addres.Should().Be(client.Addres); dClient.Name.Should().Be(client.Name); dClient.Orders.Count.Should().Be(0); Order order = new Order(ProductType.Duvel, 5, client); DOrder dOrder = Mapper.ToDOrder(order); dOrder.Id.Should().Be(order.Id); dOrder.Product.Should().Be(order.Product); dOrder.Amount.Should().Be(order.Amount); client.AddOrder(ProductType.Leffe, 10); dClient = Mapper.ToDClient(client); dClient.Orders.Count.Should().Be(1); dClient.Orders[0].Id.Should().Be(client.Orders[0].Id); dClient.Orders[0].Product.Should().Be(client.Orders[0].Product); dClient.Orders[0].Amount.Should().Be(client.Orders[0].Amount); }
public void MapperTest_ToDomainObjects() { DClient dClient = new DClient("Test", "Test address"); Client client = Mapper.ToClient(dClient); client.Id.Should().Be(dClient.Id); client.Addres.Should().Be(dClient.Addres); client.Name.Should().Be(dClient.Name); client.Orders.Count.Should().Be(0); DOrder dOrder = new DOrder(ProductType.Duvel, 10); Order order = Mapper.ToOrder(dOrder, client); order.Id.Should().Be(dOrder.Id); order.Product.Should().Be(dOrder.Product); order.Amount.Should().Be(dOrder.Amount); order.Client.Id.Should().Be(client.Id); order.Client.Addres.Should().Be(client.Addres); order.Client.Name.Should().Be(client.Name); order.Client.Orders.Count.Should().Be(0); client.AddOrder(ProductType.Leffe, 6); Client client2 = Mapper.ToClient(Mapper.ToDClient(client)); client2.Id.Should().Be(client.Id); client2.Addres.Should().Be(client.Addres); client2.Name.Should().Be(client.Name); client2.Orders.Count.Should().Be(1); client2.Orders[0].Id.Should().Be(client.Orders[0].Id); client2.Orders[0].Product.Should().Be(client.Orders[0].Product); client2.Orders[0].Amount.Should().Be(client.Orders[0].Amount); }
/// <summary> /// Sets the id for each comic ordered. /// </summary> /// <param name="toAdd">Delivery contraining no id.</param> private void SetComicIds(DOrder toAdd) { using (var command = context.CreateCommand()) { int i = 0; foreach (var comic in toAdd.OrderComics.Keys) { command.CommandText = @$ "Select * From Series Where Series.name = @name{i}"; command.AddParameter($"name{i}", comic.Series.Name); int?seriesId = (int?)command.ExecuteScalar(); if (seriesId == null) { throw new DataException($"Series {comic.Series.Name} is not in the database"); } command.CommandText = @$ "Select * From Comics Where Comics.Title = @title{i} AND Comics.SeriesNr = @series_Nr{i} OR @series_Nr{i} IS NULL AND Comics.Series_ID = @series_Id{i};"; command.AddParameter($"title{i}", comic.Title); command.AddParameter($"series_Nr{i}", comic.SeriesNumber); command.AddParameter($"series_Id{i}", seriesId); int?id = (int?)command.ExecuteScalar(); if (id == null) { throw new DataException($"Comic {comic.Title} is not in the database"); } comic.Id = (int)id; i++; } } }
/// <summary> /// Link the Order and the stock and update the stock. Also add the ComicDelivery for the amounds. /// </summary> /// <param name="dOrder">DOrder to use.</param> private void LinkStockToOrder(DOrder dOrder) { using (var command = context.CreateCommand()) { command.AddParameter("order_Id", dOrder.Id); int i = 0; foreach (var comicPair in dOrder.OrderComics) { command.CommandText = @"Select Stock.ID From Stock " + $"where Stock.ComicID = @comic_Id{i};"; command.AddParameter($"Comic_Id{i}", comicPair.Key.Id); int StockID = (int)command.ExecuteScalar(); command.CommandText = @"insert into OrdersComics (OrderID, StockID, AmountOrdered) " + $"values (@order_Id, @stock_Id{i}, @amount{i});"; command.AddParameter($"stock_Id{i}", StockID); command.AddParameter($"amount{i}", comicPair.Value); command.ExecuteNonQuery(); command.CommandText = @"UPDATE Stock " + $"SET Stock.Stock -= @amount{i} " + $"WHERE Stock.ID = @stock_Id{i};"; command.ExecuteNonQuery(); i++; } } }
/// <summary> /// Maps Data Order object to Order object /// </summary> /// <param name="dorder">Data order to map</param> /// <returns></returns> public static Order FromDOrderToOrder(DOrder dorder) { Order order = new Order(dorder.Product, dorder.Amount, FromDClientToClient(dorder.Client)); order.Id = dorder.OrderId; return(order); }
public async Task <ActionResult <DOrder> > PostDOrder(DOrder dOrder) { _context.Order.Add(dOrder); await _context.SaveChangesAsync(); return(CreatedAtAction("GetDOrder", new { id = dOrder.Id }, dOrder)); }
public async Task <IActionResult> PostDOrder(OrderItemDTO dto) { var status = await _context.OrderStatus.FirstOrDefaultAsync(s => s.Description == "NEW"); _context.Entry(status).State = EntityState.Detached; var newOrder = new DOrder() { OpenDate = DateTime.Now, CustomerId = dto.IdCustomer, OrderStatusId = status.Id, }; _context.Order.Add(newOrder); try { await _context.SaveChangesAsync(); var idOrder = newOrder.Id; foreach (DOrderItem item in dto.Items) { item.OrderId = idOrder; _context.OrderItem.Add(item); await _context.SaveChangesAsync(); } return(Ok(new { idOrder })); } catch (Exception ex) { return(NotFound()); } }
public void AddOrder(Order order) { DOrder toAdd = Mapper.ToDOrder(order); SetComicIds(toAdd); AddDOrder(toAdd); LinkStockToOrder(toAdd); }
/// <summary> /// Add the info of a DOrder to the databse. /// </summary> /// <param name="dOrder">DOrder to add.</param> private void AddDOrder(DOrder dOrder) { using (var command = context.CreateCommand()) { command.CommandText = $@"Insert into Orders (OrderDate) " + "values (@orderdate) " + "SELECT CAST(scope_identity() AS int);"; command.AddParameter("orderdate", dOrder.Date.ToString()); dOrder.Id = (int)command.ExecuteScalar(); } }
public void UpdateOrder(Order order) { //kijk of het erinzit if (!context.Orders.Any(o => o.OrderId == order.Id)) { throw new Exception("Order not in database."); } DOrder orderToUpdate = context.Orders.Single(o => o.OrderId == order.Id); orderToUpdate.Amount = order.Amount; orderToUpdate.Product = order.Product; }
public void AddOrder(Order order, int clientId) { if (clientId <= 0) { throw new Exception("No clientId provided."); } DOrder dOrder = Mapper.FromOrderToDOrder(order); dOrder.Client = null; dOrder.Client_Id = clientId; context.Orders.Add(dOrder); }
public Order GetOrderWithoutId(Order order) { //kijk of het erinzit if (!context.Orders.Any((o => o.Amount == order.Amount && o.Product == order.Product && o.Client.Name == order.Client.Name && o.Client.Address == o.Client.Address))) { throw new Exception("Order not in database."); } DOrder dorder = context.Orders .AsNoTracking() .Include(o => o.Client) .AsNoTracking() .Single(o => o.Amount == order.Amount && o.Product == order.Product && o.Client.Name == order.Client.Name && o.Client.Address == o.Client.Address); return(Mapper.FromDOrderToOrder(dorder)); }
public Order GetOrder(int id) { //kijk of het erinzit if (!context.Orders.Any(o => o.OrderId == id)) { throw new Exception("Order not in database."); } DOrder dorder = context.Orders .AsNoTracking() .Include(o => o.Client) .AsNoTracking() .Single(o => o.OrderId == id); return(Mapper.FromDOrderToOrder(dorder)); }