Exemple #1
0
        public async Task <ResponseDto <IEnumerable <GoogleTask> > > AddRangeAsync(string taskListID, IEnumerable <GoogleTask> tasks)
        {
            return(await Task.Run(async() =>
            {
                _logger.Information($"AddRangeAsync: Trying to add {tasks.Count()} tasks to tasklist = {taskListID}");
                var response = new ResponseDto <IEnumerable <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)
                        {
                            _logger.Warning($"AddRangeAsync: Couldn't find a tasklist with id = {taskListID}");
                            response.Message = $"Couldn't find the tasklist where all the tasks will be saved in the db";
                        }
                        else
                        {
                            var entities = tasks.ToList();
                            entities.ForEach(t => t.TaskList = taskList);

                            await context.AddRangeAsync(entities);
                            response.Succeed = await context.SaveChangesAsync() > 0;
                            response.Result = entities;
                        }
                        _logger.Information("AddRangeAsync: Completed successfully");
                    }
                    catch (Exception e)
                    {
                        _logger.Error(e, "AddRangeAsync: An unknown error occurred");
                        response.Message = GetExceptionMessage(e);
                    }
                }
                return response;
            }).ConfigureAwait(false));
        }
Exemple #2
0
        public async Task <ResponseDto <IEnumerable <GoogleTaskList> > > AddRangeAsync(IEnumerable <GoogleTaskList> entities)
        {
            return(await Task.Run(async() =>
            {
                _logger.Information($"AddRangeAsync: Trying to add {entities.Count()} task lists");
                var response = new ResponseDto <IEnumerable <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("AddRangeAsync: Couldn't find the current active user in the db");
                        }
                        else
                        {
                            var e = entities.ToList();
                            e.ForEach(tl => tl.User = currentUser);

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