public ActionResult Post([FromBody] Category model) { var category = new Category(); category.Name = model.Name; category.IsActive = true; _db.Add(category); _db.SaveChanges(); return(Ok(category)); }
public async Task <IActionResult> Create([Bind("ID,Name,SiteUserId")] Course course) { if (ModelState.IsValid) { _context.Add(course); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["SiteUserId"] = new SelectList(_context.Siteusers, "ID", "Name", course.SiteUserId); return(View(course)); }
public async Task CreateOrderAsync(Order order) { var drink = await db.Drinks.FindAsync(order.Drink.CodDrink); OrderEntity orderEntity = new OrderEntity { Drink = drink, IsWinner = order.IsWinner, OrderDate = DateTime.UtcNow, Rest = order.Rest }; db.Add(orderEntity); await db.SaveChangesAsync(); }
public void Can_write_container() { //Arrange var newContainer = new Container(2); newContainer.UpdateCurrentCapacity(1); //Act var ex = Record.Exception(() => { _context.Add(newContainer); _context.SaveChanges(); }); //Assert Assert.Null(ex); }
public async Task <bool> PostAsync([FromBody] Dictionary <string, string> data) { var keys = data.Keys; var entities = await context.Configs.Where(x => keys.Contains(x.Key)).ToListAsync(); context.RemoveRange(entities); foreach (var kv in data) { var entity = new ConfigEntity { Key = kv.Key, Value = kv.Value }; context.Add(entity); } var result = await context.SaveChangesAsync(); return(result > 0); }
public async Task FindAll_return_expected_users_when_called() { var options = new DbContextOptionsBuilder <DemoContext>() .UseInMemoryDatabase(databaseName: "find_all_users") .Options; using (var context = new DemoContext(options)) { context.Add(new User("Alex", new DateTime(1984, 06, 24))); await context.SaveChangesAsync(); } using (var context = new DemoContext(options)) { var repository = new UserRepository(context); var users = await repository.FindAll(); Assert.Single(users); Assert.Contains(users, user => user.Name.Equals("Alex")); } }
public async Task Put(User user) { _db.Add(user); await _db.SaveChangesAsync(); }
public PlayerChallengeRespModel ChallengePlayer(PlayerChallengeReqModel reqModel) { var result = new PlayerChallengeRespModel(); var game = new Game(); var boardList = new List <Board>(); List <PointCoordinate> pointCoordinateList; var player = _context.Players.FirstOrDefault(x => x.Id == reqModel.PlayerId); var opponent = _context.Players.FirstOrDefault(x => x.Id == reqModel.OpponentId); //Check if players exists if (player != null && opponent != null) { using (var transaction = _context.Database.BeginTransaction()) { try { //Create Game game.CreatedUTC = DateTime.UtcNow; game.ModifiedUTC = DateTime.UtcNow; game.PlayerId = reqModel.PlayerId; game.OpponentId = reqModel.OpponentId; game.GameStatusId = (int)Enums.GameStatus.InProgress; _context.Add(game); _context.SaveChanges(); //Create Boards boardList.Add(new Board() { CreatedUTC = DateTime.UtcNow, ModifiedUTC = DateTime.UtcNow, PlayerId = reqModel.PlayerId, GameId = game.Id }); boardList.Add(new Board() { CreatedUTC = DateTime.UtcNow, ModifiedUTC = DateTime.UtcNow, PlayerId = reqModel.OpponentId, GameId = game.Id }); foreach (var board in boardList) { _context.Add(board); } _context.SaveChanges(); //Create PointCoordinates foreach (var board in boardList) { pointCoordinateList = SetRandomShipPoints(board.Id); foreach (var point in pointCoordinateList) { _context.Add(point); } } _context.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); } result.Data = new ChallengePlayerData() { PlayerId = reqModel.PlayerId, OpponentId = reqModel.OpponentId, GameId = game.Id, StartingPlayer = RandomStartPlayer(reqModel.PlayerId, reqModel.OpponentId) }; } } else { result.Error = new SimpleError() { ErrorArg = player == null?reqModel.PlayerId.ToString() : reqModel.OpponentId.ToString(), ErrorCode = "error.unknown-user-id" }; } return(result); }
public List <Blog> CreateDemoData() { List <Blog> demoBlogs = new List <Blog> () { new Blog() { Url = "http://www.youtube.com" }, new Blog() { Url = "http://www.google.com" }, new Blog() { Url = "http://www.microsoft.com" }, new Blog() { Url = "http://www.azure.com" } }; List <Author> demoAuthors = new List <Author> () { new Author() { Name = "Bob" }, new Author() { Name = "Peter" } }; using (var db = new DemoContext()) { foreach (var blog in demoBlogs) { blog.Posts.AddRange(new List <Post> () { new Post() { Title = "Post1", Content = "Lorem Ipsum", Author = demoAuthors[0] }, new Post() { Title = "Post2", Content = "Lorem Ipsum", Author = demoAuthors[1] }, new Post() { Title = "Post3", Content = "Lorem Ipsum", Author = demoAuthors[0] }, new Post() { Title = "Post4", Content = "Lorem Ipsum", Author = demoAuthors[1] } }); db.Add(blog); db.SaveChanges(); } return(db.Blogs.ToList()); } }