Merge branch 'feat/backend'

This commit is contained in:
ChronosX88 2019-09-25 19:51:25 +04:00
commit 9a06f282bb
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
3 changed files with 37 additions and 9 deletions

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using InstructorsListApp.Models;
using Microsoft.EntityFrameworkCore;
namespace InstructorsListApp.Controllers
{
@ -23,15 +24,28 @@ namespace InstructorsListApp.Controllers
return databaseContext.Instructors.ToList();
}
[HttpGet("{id}")]
public IActionResult GetInstructorByID(string id) {
var instructor = databaseContext.Instructors.FirstOrDefault(i => i.Id == id);
if(instructor != null) {
databaseContext.Entry<Instructor>(instructor).State = EntityState.Detached; // remove unneccessary refs
return Ok(instructor);
}
return NotFound(new Error(Error.EntryIsNotFound, "No such instructor"));
}
[HttpPost("add")]
public IActionResult CreateInstructorEntry([FromBody] Instructor instructor) {
if(ModelState.IsValid) {
if(instructor.FirstName == null && instructor.LastName == null && instructor.MiddleName == null) {
return BadRequest(new Error(Error.PostBodyIsNotValid, "Given model isn't valid!"));
}
instructor.Id = Guid.NewGuid().ToString();
databaseContext.Instructors.Add(instructor);
databaseContext.SaveChanges();
return Ok(instructor.Id);
}
return BadRequest(ModelState);
return BadRequest(new Error(Error.PostBodyIsNotValid, "Given model isn't valid!"));
}
[HttpPut("{id}")]
@ -40,12 +54,12 @@ namespace InstructorsListApp.Controllers
instructor.Id = id;
var instructorEntry = databaseContext.Instructors.FirstOrDefault(i => i.Id == id);
if(instructorEntry != null) {
databaseContext.Entry<Instructor>(instructorEntry).State = Microsoft.EntityFrameworkCore.EntityState.Detached;
databaseContext.Entry<Instructor>(instructorEntry).State = EntityState.Detached; // remove unneccessary refs
databaseContext.Instructors.Update(instructor);
databaseContext.SaveChanges();
return Ok();
}
return NotFound("Given instructor isn't found");
return NotFound(new Error(Error.EntryIsNotFound, "No such instructor"));
}
return BadRequest(ModelState);
}
@ -58,7 +72,7 @@ namespace InstructorsListApp.Controllers
databaseContext.SaveChanges();
return Ok();
}
return NotFound("No such instructor");
return NotFound(new Error(Error.EntryIsNotFound, "No such instructor"));
}
}
}

14
Models/Error.cs Normal file
View File

@ -0,0 +1,14 @@
namespace InstructorsListApp.Models {
public class Error {
public static int EntryIsNotFound = 0x0;
public static int PostBodyIsNotValid = 0x1;
public int errCode {get; set;}
public string errText {get; set;}
public Error(int errCode, string errText) {
this.errCode = errCode;
this.errText = errText;
}
}
}

View File

@ -1,8 +1,8 @@
namespace InstructorsListApp.Models {
public class Instructor {
public string Id {get; set;}
public string FirstName {get;set;}
public string MiddleName {get;set;}
public string LastName {get;set;}
}
public string Id {get; set;}
public string FirstName {get;set;}
public string MiddleName {get;set;}
public string LastName {get;set;}
}
}