コード例 #1
0
        public async Task <IActionResult> update(int id, [FromBody] Models.Task model)
        {
            if (!ModelState.IsValid)
            {
                var errors = ModelState.Select(x => x.Value.Errors)
                             .Where(y => y.Count > 0)
                             .ToList();
                return(BadRequest(errors));
            }

            var found = await context.Tasks.FindAsync(id);

            if (found != null)
            {
                found.Title     = model.Title;
                found.Desc      = model.Desc;
                found.DeadLine  = model.DeadLine;
                found.Status    = 100;
                found.UpdatedAt = DateTime.Now;
                await context.SaveChangesAsync();

                return(Ok(found));
            }
            return(BadRequest("Không tồn tại task."));
        }
コード例 #2
0
        public async Task <IActionResult> AddUser(int id, [FromBody] Models.Task model)
        {
            if (!ModelState.IsValid)
            {
                var errors = ModelState.Select(x => x.Value.Errors)
                             .Where(y => y.Count > 0)
                             .ToList();
                return(BadRequest(errors));
            }

            var found = await context.Tasks.FindAsync(id);

            if (found != null)
            {
                /*==============================
                *    Update table UserTasks
                *    1. Delete
                *    2. Insert
                *    ==============================*/

                /* =========== 1 ==========*/

                var query = context.UserTasks.AsQueryable();
                query = query.Where(item => item.TaskId == id);
                var data = await query.ToListAsync();

                foreach (var item in data)
                {
                    context.UserTasks.Remove(item);
                    await context.SaveChangesAsync();
                }

                /* =========== 2 ==========*/

                if (model.UserTasks.Count > 0)
                {
                    await context.UserTasks.AddRangeAsync(model.UserTasks);

                    await context.SaveChangesAsync();
                }

                return(Ok(model.UserTasks));
            }
            return(BadRequest("Không tồn tại task"));
        }
コード例 #3
0
        public async Task <IActionResult> Create([FromBody] Models.Task model)
        {
            if (!ModelState.IsValid)
            {
                var errors = ModelState.Select(x => x.Value.Errors)
                             .Where(y => y.Count > 0)
                             .ToList();
                return(BadRequest(errors));
            }

            model.Status    = 0;
            model.CreatedAt = DateTime.Now;
            model.UpdatedAt = DateTime.Now;
            model.DeadLine  = DateTime.Now.AddDays(1);

            await context.Tasks.AddAsync(model);

            await context.SaveChangesAsync();

            return(Ok(model));
        }