Skip to content

Commit

Permalink
JSON POSTs
Browse files Browse the repository at this point in the history
  • Loading branch information
Petru Faurescu committed Feb 20, 2018
1 parent af9e70f commit 7ae7605
Show file tree
Hide file tree
Showing 8 changed files with 977 additions and 1,002 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -10,3 +10,4 @@
/obj
/UpgradeLog.htm
.vs/config/applicationhost.config
NotebookAppApi.csproj.user
1,916 changes: 937 additions & 979 deletions .vs/config/applicationhost.config

Large diffs are not rendered by default.

25 changes: 11 additions & 14 deletions Controllers/NotesController.cs
Expand Up @@ -21,33 +21,30 @@ public NotesController(INoteRepository noteRepository)

[NoCache]
[HttpGet]
public Task<IEnumerable<Note>> Get()
{
return GetNoteInternal();
}

private async Task<IEnumerable<Note>> GetNoteInternal()
public async Task<IEnumerable<Note>> Get()
{
return await _noteRepository.GetAllNotes();
}

// GET api/notes/5
[HttpGet("{id}")]
public Task<Note> Get(string id)
{
return GetNoteByIdInternal(id);
}

private async Task<Note> GetNoteByIdInternal(string id)
public async Task<Note> Get(string id)
{
return await _noteRepository.GetNote(id) ?? new Note();
}

// POST api/notes
[HttpPost]
public void Post([FromBody]string value)
public void Post([FromBody] NoteParam newNote)
{
_noteRepository.AddNote(new Note() { Body = value, CreatedOn = DateTime.Now, UpdatedOn = DateTime.Now });
_noteRepository.AddNote(new Note
{
Id = newNote.Id,
Body = newNote.Body,
CreatedOn = DateTime.Now,
UpdatedOn = DateTime.Now,
UserId = newNote.UserId
});
}

// PUT api/notes/5
Expand Down
2 changes: 0 additions & 2 deletions Controllers/SystemController.cs
Expand Up @@ -4,8 +4,6 @@
using NotebookAppApi.Interfaces;
using NotebookAppApi.Model;

// For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace NotebookAppApi.Controllers
{
[Route("api/[controller]")]
Expand Down
16 changes: 13 additions & 3 deletions Data/NoteRepository.cs
Expand Up @@ -32,14 +32,15 @@ public async Task<IEnumerable<Note>> GetAllNotes()
}
}

// query after internal or internal id
//
public async Task<Note> GetNote(string id)
{
var filter = Builders<Note>.Filter.Eq("Id", id);

try
{
ObjectId internalId = GetInternalId(id);
return await _context.Notes
.Find(filter)
.Find(note => note.Id == id || note.InternalId == internalId)
.FirstOrDefaultAsync();
}
catch (Exception ex)
Expand All @@ -49,6 +50,15 @@ public async Task<Note> GetNote(string id)
}
}

private ObjectId GetInternalId(string id)
{
ObjectId internalId;
if (!ObjectId.TryParse(id, out internalId))
internalId = ObjectId.Empty;

return internalId;
}

public async Task AddNote(Note item)
{
try
Expand Down
6 changes: 4 additions & 2 deletions Model/Note.cs
@@ -1,13 +1,15 @@
using System;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

namespace NotebookAppApi.Model
{
public class Note
{
[BsonId]
public string Id { get; set; }
public string Body { get; set; } = string.Empty;
public ObjectId InternalId { get; set; } // standard BSonId generated by MongoDb
public string Id { get; set; } // external Id, easier to reference: 1,2,3 etc.
public string Body { get; set; } = string.Empty;
public DateTime UpdatedOn { get; set; } = DateTime.Now;
public DateTime CreatedOn { get; set; } = DateTime.Now;
public int UserId { get; set; } = 0;
Expand Down
9 changes: 9 additions & 0 deletions Model/NoteParam.cs
@@ -0,0 +1,9 @@
namespace NotebookAppApi.Model
{
public class NoteParam
{
public string Id { get; set; } = string.Empty;
public string Body { get; set; } = string.Empty;
public int UserId { get; set; } = 0;
}
}
4 changes: 2 additions & 2 deletions NotebookAppApi.csproj
Expand Up @@ -9,8 +9,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="MongoDB.Driver" Version="2.4.4" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
<PackageReference Include="MongoDB.Driver" Version="2.5.0" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 7ae7605

Please sign in to comment.