Exemple #1
0
 public void Validate(ValidationContext context)
 {
     if (string.IsNullOrWhiteSpace(Content))
     {
         context.Required(nameof(Content));
     }
 }
Exemple #2
0
        public async Task<ActionResult> Create(CreateNoteViewModel model)
        {
            var validationContext = new ValidationContext();
            model.Validate(validationContext);

            if (validationContext.HasErrors)
            {
                AddModelStateError(validationContext);
                return View(model);
            }

            try
            {
                var noteCreatedViewModel = await model.CreateNote();

                Response.SetCookie(new HttpCookie(
                    "EditKey_" + noteCreatedViewModel.NoteId,
                    noteCreatedViewModel.EditKey));
                return RedirectToAction("Get", new { noteId = noteCreatedViewModel.NoteId });
            }
            catch (ValidationException ve)
            {
                AddModelStateError(ve.ValidationContext);
                return View(model);
            }
        }
        public void Validate(ValidationContext context)
        {
            if (string.IsNullOrWhiteSpace(Content))
            {
                context.Required(nameof(Content));
            }            

            if (ExpirationValue < 1)
            {
                context.AddError(nameof(ExpirationValue), "Expiration value must be greater than zero.");
            }
        }        
Exemple #4
0
        public void Validate(ValidationContext context)
        {
            if (string.IsNullOrWhiteSpace(NoteId))
            {
                context.Required(nameof(NoteId));
            }

            if (string.IsNullOrWhiteSpace(EditKey))
            {
                context.Required(nameof(EditKey));
            }
        }
Exemple #5
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,
            };
        }
Exemple #6
0
        public async Task<ActionResult> Edit(string noteId, string editKey, EditNoteViewModel model)
        {
            var validationContext = new ValidationContext();
            model.Validate(validationContext);

            if (validationContext.HasErrors)
            {
                AddModelStateError(validationContext);
                return View(model);
            }

            try
            {
                var updateResponse = await model.UpdateNote(noteId, editKey);

                if (updateResponse.IsSuccessful)
                {
                    return RedirectToAction("Get", new { noteId = noteId });
                }
                else
                {
                    return new HttpStatusCodeResult(updateResponse.StatusCode);
                }
            }
            catch (ValidationException ve)
            {
                AddModelStateError(ve.ValidationContext);
                return View(model);
            }
        }
Exemple #7
0
        private void AddModelStateError(ValidationContext validationContext)
        {
            foreach (var field in validationContext.ErrorFields)
            {
                foreach (var msg in validationContext.GetErrors(field))
                {
                    ModelState.AddModelError(field, msg);
                }
            }

            foreach (var msg in validationContext.GetNonFieldErrors())
            {
                ModelState.AddModelError("", msg);
            }
        }
Exemple #8
0
        public async Task<Response> DeleteNote(DeleteNoteRequest 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 noteEntity = await GetEntityForEditing(request.NoteId, request.EditKey, cancellationToken);
            if (noteEntity == null)
            {
                return new Response { StatusCode = HttpStatusCode.NotFound };
            }

            try
            {
                await this.noteStore.Delete(noteEntity, cancellationToken);
                return new Response { StatusCode = HttpStatusCode.OK };
            }
            catch (ETagMismatchException)
            {
                return new Response { StatusCode = HttpStatusCode.PreconditionFailed };
            }
        }
Exemple #9
0
        public async Task<Response> UpdateNote(UpdateNoteRequest 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);
            }

            // if there's an etag mismatch exception, retry up to 3 times
            for (int i = 0; i < 3; i++)
            {
                var noteEntity = await GetEntityForEditing(request.NoteId, request.EditKey, cancellationToken);
                if (noteEntity == null)
                {
                    return new Response { StatusCode = HttpStatusCode.NotFound };
                }

                noteEntity.Title = request.Title;
                noteEntity.Content = request.Content;
                noteEntity.NoteType = request.NoteType;

                try
                {
                    await this.noteStore.Update(noteEntity, cancellationToken);
                    return new Response { StatusCode = HttpStatusCode.OK };
                }
                catch (ETagMismatchException)
                {
                }
            }

            return new Response { StatusCode = HttpStatusCode.PreconditionFailed };
        }
Exemple #10
0
 public ValidationException(ValidationContext validationContext)
 {
     ValidationContext = validationContext;
 }