Exemplo n.º 1
0
        public async Task <IActionResult> PutGoal(int id, Goal goal)
        {
            if (id != goal.GoalId)
            {
                return(BadRequest());
            }

            _context.Entry(goal).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!GoalExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 2
0
        public async Task <ActionResult <Goal> > PostGoal(Goal goal)
        {
            _context.Goals.Add(goal);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetGoal), new { id = goal.GoalId }, goal));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Create([Bind("Id,Title")] Goal goal)
        {
            if (ModelState.IsValid)
            {
                _context.Add(goal);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(goal));
        }
Exemplo n.º 4
0
        public async Task TestMethod_UsingSqliteInMemoryProvider_Success()
        {
            using (var connection = new SqliteConnection("DataSource=:memory:"))
            {
                connection.Open();

                var options = new DbContextOptionsBuilder <GoalContext>()
                              .UseSqlite(connection) // Set the connection explicitly, so it won't be closed automatically by EF
                              .Options;

                // Create the dabase schema
                // You can use MigrateAsync if you use Migrations
                using (var context = new GoalContext(options))
                {
                    await context.Database.EnsureCreatedAsync();
                } // The connection is not closed, so the database still exists

                using (var context = new GoalContext(options))
                {
                    Goal goal = new Goal();
                    goal.Name = "test goal";
                    goal.Id   = 1;


                    //var user = new User() { Email = "*****@*****.**" };
                    context.Goals.Add(goal);
                    await context.SaveChangesAsync();
                }

                using (var context = new GoalContext(options))
                {
                    var count = await context.Goals.CountAsync();

                    Assert.Equal(1, count);

                    var u = await context.Goals.FirstOrDefaultAsync(user => user.Name == "test goal");

                    Assert.NotNull(u);
                }
            }
        }