Backend Your Backend task is to build a simple RESTful, JSON API to power a note
ID: 3883777 • Letter: B
Question
Backend Your Backend task is to build a simple RESTful, JSON API to power a note-taking application. Guidelines The API should be implemented in Java, C#, or C++. Any frameworks you use are up to you. I should be able to download your code from github.com and run it on my machine, so you should include instructions for setup/configuration. The easier it is for me to setup and deploy, the better. The notes API should live at the route /api/notes . So, if your API server is running on localhost, I would expect to access the 'notes' API at http://localhost/api/notes The Note Model "body" "Ask Larry about the TPS reports." Create a New Note When I POST note JSON to the notes route, a new note will be created. POST /api/notes BODY a note Returns: a saved note.. curl -i -H "Content-Type: application/json" -x POST -d '"body""Pick up milk!"'h ttp://localhost/api/notesExplanation / Answer
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Core.Objects;
using System.Linq;
public class NotesController : ApiController{
//Add the data to the note object
Note[] notes = new Note[]{
new Note{ id = 1, body = "Ask Larry About TPS Reports"},
new Note{ id = 2, body = "Ask Jhon About WFH Reports"}
}
};
[HttpGet]
public IEnumerable<Employee> notes(){
return notes; // Return the list of the Notes
}
[HttpPost]
public string notes(Notes objNotes)
{
//create DBContext object
using (var dbCtx = new NotesDBEntities())
{
//Add Notes object into Notes DBset
dbCtx.Notes.Add(objNotes);
// call SaveChanges method to save note into database
dbCtx.SaveChanges();
}
}
[HttpGet]
public IHttpActionResult notes(int id){
var note = notes.FirstOrDefault((p) => p.id == id); // Retrive the particular record from the notes list
if (note == null){
return NotFound();
}
return Ok(note);
}
}
Creating Note Model:
public class Note
{
public int id { get; set; }
public string body { get; set; }
}
Creating the Notes DBContext:
public partial class NotesDBEntities : DbContext
{
public NotesDBEntities ()
: base("name=NotesDBEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
public virtual DbSet<Note> Notes { get; set; }
}
In Web.config add the below code:
<connectionStrings>
<add name="NotesDBEntities" connectionString="Server=localhost;
Database=Notes; Integrated Security=True"/> // Here Notes is the Database
</connectionStrings >
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.