public async Task <ResponseDto <GoogleUser> > AddAsync(GoogleUser entity)
 {
     return(await Task.Run(async() =>
     {
         _logger.Information($"AddAsync: Trying to add a new user {entity.ID}");
         var response = new ResponseDto <GoogleUser>
         {
             Message = string.Empty,
             Succeed = false
         };
         using (var context = new MiraiNotesContext())
         {
             try
             {
                 await context.AddAsync(entity);
                 response.Succeed = await context.SaveChangesAsync() > 0;
                 response.Result = entity;
                 _logger.Information("AddAsync: Completed successfully");
             }
             catch (Exception e)
             {
                 _logger.Error(e, "AddAsync: An unknown error occurred");
                 response.Message = GetExceptionMessage(e);
             }
         }
         return response;
     }).ConfigureAwait(false));
 }
Exemple #2
0
        public async Task <ResponseDto <GoogleTask> > AddAsync(string taskListID, GoogleTask task)
        {
            return(await Task.Run(async() =>
            {
                _logger.Information($"AddAsync: Trying to add a new task into taskListId = {taskListID}");
                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 == taskListID);

                        if (taskList == null)
                        {
                            response.Message = $"Couldn't find the tasklist where {task.Title} is going to be saved";
                            _logger.Warning($"AddAsync: Couldn't find a tasklist with id = {taskListID}");
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(task.Position))
                            {
                                string position = await GetLastestPosition(context, taskList.GoogleTaskListID, task.ParentTask);
                                task.Position = position;
                            }
                            task.TaskList = taskList;

                            await context.AddAsync(task);
                            response.Succeed = await context.SaveChangesAsync() > 0;
                            response.Result = task;
                        }
                        _logger.Information("AddAsync: Completed successfully");
                    }
                    catch (Exception e)
                    {
                        _logger.Error(e, "AddAsync: An unknown error occurred");
                        response.Message = GetExceptionMessage(e);
                    }
                }
                return response;
            }).ConfigureAwait(false));
        }
Exemple #3
0
        public async Task <ResponseDto <GoogleTaskList> > AddAsync(GoogleTaskList entity)
        {
            return(await Task.Run(async() =>
            {
                _logger.Information("AddAsync: Trying to add a new task list");
                var response = new ResponseDto <GoogleTaskList>
                {
                    Message = string.Empty,
                    Succeed = false
                };
                using (var context = new MiraiNotesContext())
                {
                    try
                    {
                        var currentUser = await context.Users
                                          .FirstOrDefaultAsync(u => u.IsActive);

                        if (currentUser == null)
                        {
                            response.Message = "Couldn't find the current active user in the db";
                            _logger.Warning("AddAsync: Couldn't find the current active user in the db");
                        }
                        else
                        {
                            entity.User = currentUser;
                            await context.AddAsync(entity);
                            response.Succeed = await context.SaveChangesAsync() > 0;
                            response.Result = entity;
                        }
                        _logger.Information("AddAsync: Completed successfully");
                    }
                    catch (Exception e)
                    {
                        _logger.Error(e, "AddAsync: An unknown error occurred");
                        response.Message = GetExceptionMessage(e);
                    }
                }
                return response;
            }).ConfigureAwait(false));
        }