Beispiel #1
0
        public async Task <ServiceActionResult <RemovedTasks> > DeleteTask(int taskId)
        {
            using (var db = new ThingsToDoAppContext())
            {
                var entity = await db.Tasks.Where(x => x.Id == taskId).FirstOrDefaultAsync();

                var removedTask = new RemovedTasks
                {
                    TaskId      = taskId,
                    Title       = entity.Title,
                    Priority    = entity.Priority,
                    CategoryId  = entity.CategoryId,
                    RemovedDate = DateTime.UtcNow,
                };

                db.RemovedTasks.Add(removedTask);

                var result = await db.SaveChangesAsync();

                db.Tasks.Remove(entity);

                result = await db.SaveChangesAsync();

                if (result > 0)
                {
                    return(new ServiceActionResult <RemovedTasks>(removedTask, ServiceActionStatus.Deleted));
                }
                else
                {
                    return(new ServiceActionResult <RemovedTasks>(removedTask, ServiceActionStatus.Error));;
                }
            }
        }
Beispiel #2
0
        public async Task <List <TaskModel> > GetAllFinishedTasksAsync(int userId)
        {
            var finishedTasks = new List <TaskModel>();

            using (var db = new ThingsToDoAppContext())
            {
                finishedTasks = await db.Tasks.Include(x => x.Category).Where(x => x.Category.UserId == userId && x.FinishedDate.HasValue).Select(x => new TaskModel
                {
                    Id           = x.Id,
                    Title        = x.Title,
                    Description  = x.Description,
                    FromDate     = x.FromDate,
                    ToDate       = x.ToDate,
                    FinishedDate = x.FinishedDate.Value,
                    CategoryName = x.Category.Title,
                    Priority     = x.Priority
                }).OrderBy(x => x.FinishedDate).ToListAsync();

                if (finishedTasks.Any())
                {
                    foreach (var task in finishedTasks)
                    {
                        task.PriorityName = Common.Helpers.CheckPriority.CheckTaskPriority(task.Priority);
                    }
                }
            }

            return(finishedTasks);
        }
Beispiel #3
0
        public async Task <List <TaskModel> > GetAllRemovedTasksAsync(int userId)
        {
            var removedTasks = new List <TaskModel>();

            using (var db = new ThingsToDoAppContext())
            {
                removedTasks = await db.RemovedTasks.Include(x => x.Category).Where(x => x.Category.UserId == userId).Select(x => new TaskModel
                {
                    Id           = x.TaskId,
                    Title        = x.Title,
                    RemovedDate  = x.RemovedDate,
                    CategoryName = x.Category.Title,
                    Priority     = x.Priority
                }).OrderBy(x => x.RemovedDate).ToListAsync();

                if (removedTasks.Any())
                {
                    foreach (var task in removedTasks)
                    {
                        task.PriorityName = Common.Helpers.CheckPriority.CheckTaskPriority(task.Priority);
                    }
                }
            }

            return(removedTasks);
        }
Beispiel #4
0
        public async Task <ServiceActionResult <Tasks> > FinishTask(int taskId)
        {
            using (var db = new ThingsToDoAppContext())
            {
                var task = await db.Tasks.FirstOrDefaultAsync(x => x.Id == taskId);

                if (task != null)
                {
                    task.FinishedDate = DateTime.UtcNow;
                }

                var result = await db.SaveChangesAsync();

                if (result > 0)
                {
                    return(new ServiceActionResult <Tasks>(task, ServiceActionStatus.Finished));
                }
                else
                {
                    return(new ServiceActionResult <Tasks>(task, ServiceActionStatus.Error));
                }
            }
        }