Example #1
0
        public async Task<CreateNoteResponse> CreateNote(CreateNoteRequest request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var validationContext = new ValidationContext();
            request.Validate(validationContext);

            if (validationContext.HasErrors)
            {
                throw new ValidationException(validationContext);
            }

            var editKey = Guid.NewGuid().ToString("n");
            var noteEntity = new NoteEntity
            {
                NoteId = GetShortId(),
                Title = request.Title,
                Content = request.Content,
                ExpirationTime = request.ExpiresOn,
                NoteType = request.NoteType,
                CreationTime = Clock.UtcNow,
                EditKeyHash = editKey.Sha256(),
            };

            bool duplicateKeyException = false;
            try
            {
                await this.noteStore.Insert(noteEntity, cancellationToken);
            }
            catch (DuplicateKeyException)
            {
                duplicateKeyException = true;
            }

            if (duplicateKeyException)
            {
                // try again with a GUID ID
                noteEntity.NoteId = Guid.NewGuid().ToString("n");
                await this.noteStore.Insert(noteEntity, cancellationToken);
            }

            return new CreateNoteResponse
            {
                StatusCode = HttpStatusCode.Created,
                NoteId = noteEntity.NoteId,
                EditKey = editKey,
            };
        }
Example #2
0
        public async Task<NoteCreatedViewModel> CreateNote()
        {
            var createNoteRequest = new CreateNoteRequest
            {
                Title = Title,
                Content = Content,
                NoteType = NoteType,
                ExpiresOn = GetExpirationTime(),
            };

            var noteManager = new NoteManager(Resolver.Get<KeyValueStore<NoteEntity>>());
            var createNoteResponse = await noteManager.CreateNote(createNoteRequest, CancellationToken.None);

            if (createNoteResponse.IsSuccessful)
            {
                return new NoteCreatedViewModel
                {
                    NoteId = createNoteResponse.NoteId,
                    EditKey = createNoteResponse.EditKey,
                };
            }

            throw new ApplicationException("Fail to create note");
        }