예제 #1
0
        private async Task FetchAllResultsAsync(Domain.Synchronization.Synchronization synchronization, NoteMeContext context, CancellationToken cts)
        {
            cts.ThrowIfCancellationRequested();

            var syncDate = synchronization.LastSynchronization;
            var hasMore  = false;
            var allNotes = new List <Note>();

            do
            {
                var filterBy = $@"{nameof(Note.CreatedAt)} > ""{syncDate}""";
                var orderBy  = $"{nameof(Note.CreatedAt)}-desc";
                var query    = new GetNotesQuery()
                               .SetNormalWhere(filterBy)
                               .SetNormalOrderBy(orderBy);

                var notes = await _webService.SendAsync <PaginationDto <NoteDto> >(HttpMethod.Get,
                                                                                   Endpoints.Notes._ + query.ToUri());

                if (!notes.Data.Any() && !allNotes.Any())
                {
                    return;
                }

                hasMore = notes.Data.Count == query.PageSize;

                foreach (var noteDto in notes.Data)
                {
                    var note = await context.Notes.FirstOrDefaultAsync(x => x.Id == noteDto.Id, cts);

                    if (note != null)
                    {
                        note.Content = noteDto.Content;
                        note.Tags    = noteDto.Tags;
                        note.Name    = noteDto.Name;
                    }
                    else
                    {
                        note = _mapper.MapTo <Note>(noteDto);

                        if (note.Status == StatusEnum.Normal)
                        {
                            allNotes.Add(note);
                        }

                        await context.AddRangeAsync(note);
                    }
                }
            } while (hasMore);

            await context.SaveChangesAsync(cts);

            NPublisher.PublishIt(new NewNotesMessage(allNotes));
        }
        public async Task HandleAsync(CreateNoteInSqliteCommand command)
        {
            using (var context = _factory.CreateContext())
            {
                var note = _mapper.MapTo <Note>(command);
                note.Id = Guid.NewGuid();
                note.NeedSynchronization   = true;
                note.StatusSynchronization = SynchronizationStatusEnum.NeedInsert;

                try
                {
                    var(lot, lat) = await _geolocationService.GeLocationAsync();

                    note.Latitude  = lat;
                    note.Longitude = lot;
                }
                catch (PermissionException)
                {
                }

                note.CreatedAt = DateTime.UtcNow;

                await context.AddAsync(note);

                await context.SaveChangesAsync();

                NPublisher.PublishIt(new NewNotesMessage(note));
            }
        }
        public async Task HandleAsync(UserRegisterCommand command)
        {
            await _apiWebService.SendAsync <UserDto>(HttpMethod.Post, Endpoints.Users, command);

            var loginCommand = _mapper.MapTo <LoginCommand>(command);

            await HandleAsync(loginCommand);
        }
예제 #4
0
        private async Task FetchAllAttachmentsAsync(Domain.Synchronization.Synchronization synchronization, NoteMeContext context,
                                                    CancellationToken cts)
        {
            cts.ThrowIfCancellationRequested();

            var syncDate = synchronization.LastSynchronization;
            var hasMore  = false;

            do
            {
                var filterBy = $@"{nameof(Attachment.CreatedAt)} > ""{syncDate}""";
                var orderBy  = $"{nameof(Attachment.CreatedAt)}-desc";
                var query    = new GetAttachmentQuery()
                               .SetNormalWhere(filterBy)
                               .SetNormalOrderBy(orderBy);

                var items = await _apiWebService.SendAsync <PaginationDto <AttachmentDto> >(HttpMethod.Get,
                                                                                            Endpoints.Attachments._ + query.ToUri());

                if (!items.Data.Any())
                {
                    return;
                }

                hasMore = items.Data.Count == query.PageSize;

                foreach (var itemDto in items.Data)
                {
                    var item = await context.Attachments.FirstOrDefaultAsync(x => x.Id == itemDto.Id, cts);

                    if (item != null)
                    {
                        continue;
                    }

                    item = _mapper.MapTo <Attachment>(itemDto);
                    item.StatusSynchronization = SynchronizationStatusEnum.Ok;

                    await context.AddRangeAsync(item);
                }
            } while (hasMore);

            await context.SaveChangesAsync(cts);
        }
예제 #5
0
        public async Task <ICollection <Note> > HandleAsync(GetActiveNotesQuery query)
        {
            using (var context = _factory.CreateContext())
            {
                var list = await context.Notes
                           .Where(x => x.Status == StatusEnum.Normal)
                           .OrderByDescending(x => x.CreatedAt)
                           .Skip(query.Page * query.PageSize)
                           .Take(query.PageSize)
                           .ToListAsync();

                return(_mapper.MapTo <ICollection <Note> >(list));
            }
        }