public async Task <IHttpActionResult> Put(TicketDTO value) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } using (var dbcontext = unitOfWorkFactory.CreateTransactionalUnitOfWork()) { try { var answer = await Task.Run(() => ticketsModel.BuyTicket(value, dbcontext)); if (!answer) { return(Conflict()); } //await dbcontext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { return(StatusCode(HttpStatusCode.Conflict)); } dbcontext.Accept(); return(StatusCode(HttpStatusCode.NoContent)); } }
public void BuyTicketTest() { // Arrange var data = unitOfFactory.CreateTransactionalUnitOfWork(); User user = new User() { Login = "******", Password = "******", Tickets = "1,2,3:1-14,15,16;1,1,3,3:1-24,25,26,27" }; data.Users.Add(user); data.SaveChanges(); DiscountsController discountController = new DiscountsController(unitOfFactory); var discountResult = discountController.GetDiscount(1); var discount = (discountResult.Result as OkNegotiatedContentResult <DiscountDTO>).Content; Dictionary <DiscountDTO, int> dict = new Dictionary <DiscountDTO, int>(); dict.Add(discount, 2); ConnectionPathDTO conPath = new ConnectionPathDTO(); Connection con = data.Connections.Find(1); ConnectionDTO conDTO = Mapper.Map <Connection, ConnectionDTO>(con); ConnectionPart conPart = new ConnectionPart { Connection = con, Id = 9999999, Seats = "1;2;3", FreeSeats = 3, Route = data.Routes.Find(1), StartTime = DateTime.Now, EndTime = DateTime.Now.AddMinutes(30) }; data.ConnectionParts.Add(conPart); data.SaveChanges(); conPath.ConnectionsParts = new List <ConnectionPartDTO> { Mapper.Map <ConnectionPart, ConnectionPartDTO>(conPart) }; UserDTO userDTO = Mapper.Map <User, UserDTO>(data.Users.Where(u => u.Login == user.Login).First()); TicketDTO ticketDTO = new TicketDTO() { Discounts = dict, User = userDTO, ConnectionPath = conPath, Seats = new List <int[]> { new int[] { 1, 3 } } }; TicketsModel ticketsModel = new TicketsModel(); data.SaveChanges(); // Act ticketsModel.BuyTicket(ticketDTO, data); // Assert Assert.AreEqual("2", conPart.Seats); Assert.AreEqual(string.Format("1,2,3:1-14,15,16;1,1,3,3:1-24,25,26,27;1,1:{0}-1,3", conPart.Id), user.Tickets); }