Ejemplo n.º 1
0
 public async Task <EmptyResponseDto> RemoveAsync(GoogleTask entity)
 {
     return(await Task.Run(async() =>
     {
         _logger.Information($"RemoveAsync: Trying to delete taskId = {entity.ID}");
         var response = new EmptyResponseDto
         {
             Message = string.Empty,
             Succeed = false
         };
         using (var context = new MiraiNotesContext())
         {
             try
             {
                 context.Remove(entity);
                 response.Succeed = await context.SaveChangesAsync() > 0;
                 _logger.Information("RemoveAsync: Completed successfully");
             }
             catch (Exception e)
             {
                 _logger.Error(e, "RemoveAsync: An unknown error occurred");
                 response.Message = GetExceptionMessage(e);
             }
         }
         return response;
     }).ConfigureAwait(false));
 }
Ejemplo n.º 2
0
 public async Task <EmptyResponseDto> RemoveAsync(object id)
 {
     return(await Task.Run(async() =>
     {
         _logger.Information($"RemoveAsync: Trying to delete user with id {id}");
         var response = new EmptyResponseDto
         {
             Message = string.Empty,
             Succeed = false
         };
         using (var context = new MiraiNotesContext())
         {
             try
             {
                 var entity = await context.Users.FindAsync(id);
                 if (entity == null)
                 {
                     response.Message = "Entity couldn't be removed cause it wasnt found";
                     _logger.Warning("RemoveAsync: Task list couldn't be removed cause it wasnt found");
                 }
                 else
                 {
                     context.Remove(entity);
                     response.Succeed = await context.SaveChangesAsync() > 0;
                 }
                 _logger.Information("RemoveAsync: Completed successfully");
             }
             catch (Exception e)
             {
                 _logger.Error(e, "RemoveAsync: An unknown error occurred");
                 response.Message = GetExceptionMessage(e);
             }
         }
         return response;
     }).ConfigureAwait(false));
 }
Ejemplo n.º 3
0
        public async Task <EmptyResponseDto> RemoveTaskAsync(string taskID)
        {
            return(await Task.Run(async() =>
            {
                _logger.Information($"RemoveTaskAsync: Trying to remove taskID = {taskID}");
                var response = new EmptyResponseDto
                {
                    Message = string.Empty,
                    Succeed = false
                };
                using (var context = new MiraiNotesContext())
                {
                    try
                    {
                        var entity = await context
                                     .Tasks
                                     .FirstOrDefaultAsync(t => t.GoogleTaskID == taskID);

                        if (entity == null)
                        {
                            response.Message = "Couldn't find the task to delete";
                            _logger.Warning($"RemoveTaskAsync: Couldn't find the task with taskID = {taskID}");
                            return response;
                        }

                        var subTasks = context
                                       .Tasks
                                       .Where(t => t.ParentTask == entity.GoogleTaskID);

                        if (entity.LocalStatus == LocalStatus.CREATED)
                        {
                            context.Remove(entity);
                            if (subTasks.Count() > 0)
                            {
                                context.RemoveRange(subTasks);
                            }
                        }
                        else
                        {
                            entity.LocalStatus = LocalStatus.DELETED;
                            entity.UpdatedAt = DateTimeOffset.UtcNow;
                            entity.ToBeSynced = true;

                            context.Update(entity);

                            if (subTasks.Count() > 0)
                            {
                                await subTasks.ForEachAsync(st =>
                                {
                                    st.LocalStatus = LocalStatus.DELETED;
                                    st.UpdatedAt = DateTimeOffset.UtcNow;
                                    st.ToBeSynced = true;
                                });
                                context.UpdateRange(subTasks);
                            }
                        }
                        response.Succeed = await context.SaveChangesAsync() > 0;
                        _logger.Information("RemoveTaskAsync: Completed successfully");
                    }
                    catch (Exception e)
                    {
                        _logger.Error(e, "RemoveTaskAsync: An unknown error occurred");
                        response.Message = GetExceptionMessage(e);
                    }
                }
                return response;
            }).ConfigureAwait(false));
        }
Ejemplo n.º 4
0
        public async Task <ResponseDto <GoogleTask> > MoveAsync(string selectedTaskListID, string taskID, string parentTask, string previous)
        {
            return(await Task.Run(async() =>
            {
                _logger.Information($"MoveAsync: Trying to move the taskID = {taskID} to tasklistID {selectedTaskListID} with parentTask = {parentTask} and previousTaskID = {previous}");
                var response = new ResponseDto <GoogleTask>
                {
                    Message = string.Empty,
                    Succeed = false
                };

                using (var context = new MiraiNotesContext())
                {
                    try
                    {
                        var taskList = await context
                                       .TaskLists
                                       .FirstOrDefaultAsync(tl => tl.GoogleTaskListID == selectedTaskListID);

                        if (taskList == null)
                        {
                            response.Message = $"Couldn't find the selected task list in db";
                            _logger.Warning($"MoveAsync: Couldn't find the selected task list id = {selectedTaskListID}");
                            return response;
                        }

                        var oldEntity = await context
                                        .Tasks
                                        .FirstOrDefaultAsync(t => t.GoogleTaskID == taskID);

                        if (oldEntity == null)
                        {
                            response.Message = $"Couldn't find the task to be moved in db";
                            _logger.Warning($"MoveAsync: Couldn't find the task to be moved. TaskID = {taskID}");
                            return response;
                        }

                        string position = await GetLastestPosition(context, taskList.GoogleTaskListID, parentTask);

                        var entity = new GoogleTask
                        {
                            CompletedOn = oldEntity.CompletedOn,
                            CreatedAt = DateTimeOffset.UtcNow,
                            GoogleTaskID = Guid.NewGuid().ToString(),
                            IsDeleted = oldEntity.IsDeleted,
                            IsHidden = oldEntity.IsHidden,
                            LocalStatus = LocalStatus.CREATED,
                            Notes = oldEntity.Notes,
                            ParentTask = parentTask,
                            Position = position,
                            RemindOn = oldEntity.RemindOn,
                            RemindOnGUID = oldEntity.RemindOnGUID,
                            Status = oldEntity.Status,
                            TaskList = taskList,
                            Title = oldEntity.Title,
                            ToBeCompletedOn = oldEntity.ToBeCompletedOn,
                            ToBeSynced = true,
                            UpdatedAt = DateTimeOffset.UtcNow
                        };

                        if (oldEntity.LocalStatus == LocalStatus.CREATED)
                        {
                            context.Remove(oldEntity);
                        }
                        else
                        {
                            oldEntity.LocalStatus = LocalStatus.DELETED;
                            oldEntity.ToBeSynced = true;
                            oldEntity.UpdatedAt = DateTimeOffset.UtcNow;
                        }

                        await context.Tasks.AddAsync(entity);

                        response.Succeed = await context.SaveChangesAsync() > 0;
                        response.Result = entity;
                        _logger.Information("MoveAsync: Completed successfully");
                    }
                    catch (Exception e)
                    {
                        _logger.Error(e, "MoveAsync: An unknown error occurred");
                        response.Message = GetExceptionMessage(e);
                    }
                }
                return response;
            }).ConfigureAwait(false));
        }