public IActionResult AddLoan([FromBody] NewLoan newLoan) { Console.WriteLine("received loan POST: " + newLoan.ToString()); if (ModelState.IsValid) { var targetItem = db.Item.SingleOrDefault(i => i.Id.ToString() == newLoan.id); var loan = new Loan(); loan.Item = targetItem; targetItem.IsAvailable = false; db.Item.Update(targetItem); var identity = User.Identity as ClaimsIdentity; loan.UserId = Guid.Parse(identity.Claims.FirstOrDefault(c => c.Type == "id")?.Value); // TODO: Save a user object to this loan.User = identity.Claims.FirstOrDefault(c => c.Type == "sub")?.Value; loan.StartDate = DateTime.Today; loan.EndDate = loan.StartDate + new TimeSpan(14, 0, 0, 0); loan.Active = true; db.Loans.Add(loan); db.SaveChanges(); return(CreatedAtAction(nameof(GetLoanById), new { id = loan.Id }, loan)); } else { return(BadRequest()); } }
public IActionResult AddItem([FromBody] Item item) { if (ModelState.IsValid) { db.Item.Add(item); db.SaveChanges(); return(CreatedAtAction(nameof(GetItemById), new { id = item.Id }, item)); } else { return(BadRequest()); } }
public void FakeItems(int count) { var itemFaker = new Faker <Item>() .RuleFor(i => i.Name, f => f.Commerce.ProductName()); for (var i = 0; i < count; i++) { var item = itemFaker.Generate(); db.Item.Add(item); } db.SaveChanges(); }