예제 #1
0
        public async Task Post(HttpContext context)
        {
            var todo = await JsonSerializer.DeserializeAsync <Todo>(context.Request.Body, _options);

            _db.Todos.Add(todo);
            await _db.SaveChangesAsync();
        }
예제 #2
0
파일: TodoApi.cs 프로젝트: wenfeifei/Todos
        public async Task PostAsync(HttpContext context)
        {
            var todo = await context.Request.ReadJsonAsync <Todo>(_options);

            await _db.Todos.AddAsync(todo);

            await _db.SaveChangesAsync();
        }
예제 #3
0
        public async Task Delete(HttpContext context)
        {
            var id = (string)context.Request.RouteValues["id"];

            if (id == null || !long.TryParse(id, out var todoId))
            {
                context.Response.StatusCode = StatusCodes.Status400BadRequest;
                return;
            }

            using var db = new TodoDbContext();
            var todo = await db.Todos.FindAsync(todoId);

            if (todo == null)
            {
                context.Response.StatusCode = StatusCodes.Status404NotFound;
                await JsonSerializer.SerializeAsync(context.Response.Body, new {
                    message = "Todo Not Found"
                });

                return;
            }

            db.Todos.Remove(todo);
            await db.SaveChangesAsync();

            await JsonSerializer.SerializeAsync(context.Response.Body, new {
                message = "Todo Not Found"
            });
        }
예제 #4
0
        static async Task UpdateCompleted(HttpContext context)
        {
            if (!context.Request.RouteValues.TryGet("id", out int id))
            {
                context.Response.StatusCode = 400;
                return;
            }

            using var db = new TodoDbContext();
            var todo = await db.Todos.FindAsync(id);

            if (todo == null)
            {
                context.Response.StatusCode = 404;
                return;
            }

            var inputTodo = await context.Request.ReadJsonAsync <Todo>();

            todo.IsComplete = inputTodo.IsComplete;

            await db.SaveChangesAsync();

            context.Response.StatusCode = 204;
        }
예제 #5
0
        static async Task Post(HttpContext context)
        {
            var todo = await JsonSerializer.DeserializeAsync <Todo>(context.Request.Body, _options);

            using var db = new TodoDbContext();
            db.Todos.Add(todo);
            await db.SaveChangesAsync();
        }
예제 #6
0
        public async Task PostAsync(HttpContext context)
        {
            var todo = await context.Request.ReadFromJsonAsync <Todo>(_options);

            using var db = new TodoDbContext();
            await db.Todos.AddAsync(todo);

            await db.SaveChangesAsync();
        }
예제 #7
0
        static async Task PostAsync(HttpContext context)
        {
            var todo = await JsonSerializer.DeserializeAsync <Todo>(context.Request.Body, _options);

            using var db = new TodoDbContext();
            await db.Todos.AddAsync(todo);

            await db.SaveChangesAsync();

            context.Response.StatusCode = StatusCodes.Status204NoContent;
        }
예제 #8
0
        static async Task CreateTodo(HttpContext context)
        {
            var todo = await context.Request.ReadJsonAsync <Todo>();

            using var db = new TodoDbContext();
            await db.Todos.AddAsync(todo);

            await db.SaveChangesAsync();

            context.Response.StatusCode = 204;
        }
예제 #9
0
        public async Task Post(HttpContext context)
        {
            var todo = await JsonSerializer.DeserializeAsync <Todo>(context.Request.Body, _options);

            using var db = new TodoDbContext();
            db.Todos.Add(todo);
            await db.SaveChangesAsync();

            await JsonSerializer.SerializeAsync(context.Response.Body, new {
                message = "Successfully created Todo"
            });
        }
예제 #10
0
        public async Task <IActionResult> Delete(long id, [FromServices] TodoDbContext db)
        {
            var todo = await db.Todos.FindAsync(id);

            if (todo == null)
            {
                return(NotFound());
            }

            db.Todos.Remove(todo);
            await db.SaveChangesAsync();

            return(Ok());
        }
예제 #11
0
파일: TodoApi.cs 프로젝트: stevenknox/Todos
        public async Task DeleteAsync(TodoDbContext db, HttpContext context)
        {
            var id = (string)context.Request.RouteValues["id"];
            if (id == null || !long.TryParse(id, out var todoId))
            {
                context.Response.StatusCode = StatusCodes.Status400BadRequest;
                return;
            }

            var todo = await db.Todos.FindAsync(todoId);
            if (todo == null)
            {
                context.Response.StatusCode = StatusCodes.Status404NotFound;
                return;
            }

            db.Todos.Remove(todo);
            await db.SaveChangesAsync();
        }
예제 #12
0
        public async Task DeleteAsync(TodoDbContext db, HttpContext context)
        {
            if (!context.Request.RouteValues.TryGet("id", out int id))
            {
                context.Response.StatusCode = 400;
                return;
            }

            var todo = await db.Todos.FindAsync(id);

            if (todo == null)
            {
                context.Response.StatusCode = 404;
                return;
            }

            db.Todos.Remove(todo);
            await db.SaveChangesAsync();
        }
예제 #13
0
        static async Task UpdateTodos(HttpContext context)
        {
            var todos = await context.Request.ReadJsonAsync <IEnumerable <Todo> >();

            using var db = new TodoDbContext();
            var others = await db.Todos.ToListAsync();

            var toRemove = others.Except(todos);

            db.Todos.RemoveRange(toRemove);

            var toAdd = todos.Except(others);
            await db.Todos.AddRangeAsync(toAdd);

            var toUpdate = todos.Intersect(others);

            await db.SaveChangesAsync();

            context.Response.StatusCode = 204;
        }
예제 #14
0
        static async Task DeleteTodo(HttpContext context)
        {
            if (!context.Request.RouteValues.TryGet("id", out int id))
            {
                context.Response.StatusCode = 400;
                return;
            }

            using var db = new TodoDbContext();
            var todo = await db.Todos.FindAsync(id);

            if (todo == null)
            {
                context.Response.StatusCode = 404;
                return;
            }

            db.Todos.Remove(todo);
            await db.SaveChangesAsync();

            context.Response.StatusCode = 204;
        }
예제 #15
0
        static async Task UpdateAndAdd(HttpContext context)
        {
            if (!context.Request.RouteValues.TryGet("id", out int id))
            {
                context.Response.StatusCode = 400;
                return;
            }

            using var db = new TodoDbContext();
            var updateTodo = await db.Todos.FindAsync(id);

            updateTodo.Name = $"Update Task at {DateTime.Now}";

            var newTodo = new Todo {
                Name = "New task"
            };

            db.Add(newTodo);

            await db.SaveChangesAsync();

            context.Response.StatusCode = 200;
        }
예제 #16
0
 public async Task Post(Todo todo, [FromServices] TodoDbContext db)
 {
     db.Todos.Add(todo);
     await db.SaveChangesAsync();
 }
예제 #17
0
 public async Task Post(Todo todo)
 {
     _db.Todos.Add(todo);
     await _db.SaveChangesAsync();
 }