Пример #1
0
        public async Task UpdatesNoteWithProperIdAndPayload()
        {
            // add note to be updated
            var payload = new NoteBindingModel {
                Title = "Title", Details = "Note details"
            };
            var response = await Client.PostAsJsonAsync("/api/notes", payload);

            response.StatusCode.Should().Be(HttpStatusCode.OK);
            var note = await response.Content.ReadAsJsonAsync <NoteViewModel>();

            _noteId = note.Id;

            payload = new NoteBindingModel {
                Title = "Title Updated", Tags = new List <string> {
                    "Tag1"
                }
            };
            response = await Client.PutAsJsonAsync("/api/notes/" + note.Id, payload);

            response.StatusCode.Should().Be(HttpStatusCode.OK);
            note = await response.Content.ReadAsJsonAsync <NoteViewModel>();

            note.Id.Should().NotBeNullOrWhiteSpace();
            note.Title.Should().Be("Title Updated");
            note.Details.Should().Be("Note details");
            note.Tags.Count.Should().Be(1);
            note.Created.ToString().Should().NotBeNullOrWhiteSpace();
            note.IsFavorited.Should().BeFalse();
        }
Пример #2
0
        public async Task <IActionResult> Create([FromBody] NoteBindingModel data)
        {
            var callerId = GetIdFromClaims();

            if (data == null)
            {
                return(BadRequest("The payload must not be null."));
            }

            if (string.IsNullOrWhiteSpace(data.Title))
            {
                return(BadRequest("A title is required."));
            }

            var note = new Note
            {
                Owner       = callerId,
                Title       = data.Title,
                Details     = data.Details,
                Tags        = data.Tags,
                Created     = DateTime.Now,
                IsFavorited = false
            };
            await _noteService.Add(note);

            return(Ok(_mapper.Map <NoteViewModel>(note)));
        }
Пример #3
0
        public async Task NotFoundWhenNonExistentIdIsRequested()
        {
            var note     = new NoteBindingModel();
            var response = await Client.PutAsJsonAsync("/api/notes/xxxx", note);

            response.StatusCode.Should().Be(HttpStatusCode.NotFound);
            var responseString = await response.Content.ReadAsStringAsync();

            responseString.Should().Be("Sorry, you either have no access to the note requested or it doesn't exist.");
        }
Пример #4
0
        public async Task BadReqestIfThePayloadHasNoTitle()
        {
            var note = new NoteBindingModel {
                Title = " "
            };
            var response = await Client.PostAsJsonAsync("/api/notes", note);

            response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
            var responseString = await response.Content.ReadAsStringAsync();

            responseString.Should().Be("A title is required.");
        }
Пример #5
0
        public async Task <IHttpActionResult> Add([FromBody] NoteBindingModel noteBindingModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Note note = Mapper.Map <NoteBindingModel, Note>(noteBindingModel);

            _noteService.AddNote(note);
            await _noteService.CommitAsync();

            return(Ok());
        }
Пример #6
0
        public async Task SuccessIfThePayloadIsValid()
        {
            var payload = new NoteBindingModel {
                Title = "Note", Details = "Note details"
            };
            var response = await Client.PostAsJsonAsync("/api/notes", payload);

            response.StatusCode.Should().Be(HttpStatusCode.OK);
            var note = await response.Content.ReadAsJsonAsync <NoteViewModel>();

            note.Id.Should().NotBeNullOrWhiteSpace();
            note.Title.Should().Be("Note");
            note.Details.Should().Be("Note details");
            note.Tags.Count.Should().Be(0);
            note.Created.ToString(CultureInfo.InvariantCulture).Should().NotBeNullOrWhiteSpace();
            note.IsFavorited.Should().BeFalse();
        }
Пример #7
0
        public async Task RemoveNote()
        {
            // add note to be removed
            var payload = new NoteBindingModel {
                Title = "Title", Details = "Note details"
            };
            var response = await Client.PostAsJsonAsync("/api/notes", payload);

            response.StatusCode.Should().Be(HttpStatusCode.OK);
            var note = await response.Content.ReadAsJsonAsync <NoteViewModel>();

            response = await Client.DeleteAsync("/api/notes/" + note.Id);

            response.StatusCode.Should().Be(HttpStatusCode.OK);
            var responseString = await response.Content.ReadAsStringAsync();

            responseString.Should().Be("Note successfully removed.", responseString);
        }
Пример #8
0
        public async Task Update(string id, string owner, NoteBindingModel note)
        {
            var notesCollection = _mongoDatabase.GetCollection <Note>("notes");
            var filterBuilder   = Builders <Note> .Filter;
            var filter          = filterBuilder.Eq("_id", id) & filterBuilder.Eq("Owner", owner);
            var updateBuilder   = Builders <Note> .Update;
            var update          = updateBuilder.Set("Tags", note.Tags);

            if (!string.IsNullOrWhiteSpace(note.Title))
            {
                update = update.Set("Title", note.Title);
            }
            if (!string.IsNullOrWhiteSpace(note.Details))
            {
                update = update.Set("Details", note.Details);
            }

            await notesCollection.UpdateOneAsync(filter, update);
        }
Пример #9
0
        public async Task <IActionResult> Update(string id, [FromBody] NoteBindingModel data)
        {
            var callerId = GetIdFromClaims();

            var note = await _noteService.GetNote(id, callerId);

            if (note == null)
            {
                return(NotFound("Sorry, you either have no access to the note requested or it doesn't exist."));
            }

            if (data == null)
            {
                return(Ok(_mapper.Map <NoteViewModel>(note)));
            }

            await _noteService.Update(id, callerId, data);

            note = await _noteService.GetNote(id, callerId);

            return(Ok(_mapper.Map <NoteViewModel>(note)));
        }