[backend] feat: Implement basic CRUD operations for instructors management

This commit is contained in:
ChronosX88 2019-09-24 22:28:42 +04:00
parent 670c9a0b9c
commit 3fe1be98d8
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
9 changed files with 95 additions and 59 deletions

View File

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using InstructorsListApp.Models;
namespace InstructorsListApp.Controllers
{
[Route("api/v1/instructors")]
public class InstructorsDataController : Controller
{
DatabaseContext databaseContext;
public InstructorsDataController(DatabaseContext context)
{
databaseContext = context;
}
[HttpGet]
public IEnumerable<Instructor> GetAllInstructors()
{
return databaseContext.Instructors.ToList();
}
[HttpPost("add")]
public IActionResult CreateInstructorEntry([FromBody] Instructor instructor) {
if(ModelState.IsValid) {
instructor.Id = Guid.NewGuid().ToString();
databaseContext.Instructors.Add(instructor);
databaseContext.SaveChangesAsync();
return Ok(instructor.Id);
}
return BadRequest(ModelState);
}
[HttpPut("{id}")]
public IActionResult UpdateInstructorEntry(string id, [FromBody] Instructor instructor) {
if(ModelState.IsValid) {
databaseContext.Instructors.Update(instructor);
databaseContext.SaveChangesAsync();
return Ok();
}
return BadRequest(ModelState);
}
[HttpDelete("{id}")]
public IActionResult DelereInstructorEntry(string id) {
Instructor instructor = databaseContext.Instructors.FirstOrDefault(i => i.Id == id);
if(instructor != null) {
databaseContext.Instructors.Remove(instructor);
databaseContext.SaveChangesAsync();
return Ok();
}
return BadRequest("No such instructor");
}
}
}

View File

@ -1,44 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace instructors_crud_test_task.Controllers
{
[Route("api/[controller]")]
public class SampleDataController : Controller
{
private static string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
[HttpGet("[action]")]
public IEnumerable<WeatherForecast> WeatherForecasts()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
DateFormatted = DateTime.Now.AddDays(index).ToString("d"),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
});
}
public class WeatherForecast
{
public string DateFormatted { get; set; }
public int TemperatureC { get; set; }
public string Summary { get; set; }
public int TemperatureF
{
get
{
return 32 + (int)(TemperatureC / 0.5556);
}
}
}
}
}

12
Models/DatabaseContext.cs Normal file
View File

@ -0,0 +1,12 @@
using Microsoft.EntityFrameworkCore;
namespace InstructorsListApp.Models {
public class DatabaseContext : DbContext
{
public DatabaseContext(DbContextOptions<ApplicationContext> options)
: base(options)
{ }
public DbSet<Instructor> Instructors { get; set; }
}
}

8
Models/Instructor.cs Normal file
View File

@ -0,0 +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;}
}
}

View File

@ -8,7 +8,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace instructors_crud_test_task
namespace InstructorsListApp
{
public class Program
{

View File

@ -15,7 +15,7 @@
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"instructors_crud_test_task": {
"InstructorsListApp": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "http://localhost:5000",

View File

@ -1,12 +1,12 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SpaServices.AngularCli;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using InstructorsListApp.Models;
using Microsoft.EntityFrameworkCore;
namespace instructors_crud_test_task
namespace InstructorsListApp
{
public class Startup
{
@ -21,7 +21,13 @@ namespace instructors_crud_test_task
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
string conString = Microsoft
.Extensions
.Configuration
.ConfigurationExtensions
.GetConnectionString(this.Configuration, "DefaultDatabase");
services.AddDbContext<DatabaseContext>(options => options.UseSqlServer(conString));
//services.
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
@ -47,13 +53,6 @@ namespace instructors_crud_test_task
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
});
app.UseSpa(spa =>
{
// To learn more about options for serving an Angular SPA from ASP.NET Core,

View File

@ -4,5 +4,8 @@
"Default": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultDatabase": "Server=(localdb)\\mssqllocaldb;Database=instructorsdb;Trusted_Connection=True;"
}
}

View File

@ -10,7 +10,7 @@
<!-- Set this to true if you enable server-side prerendering -->
<BuildServerSideRenderer>false</BuildServerSideRenderer>
<RootNamespace>instructors_crud_test_task</RootNamespace>
<RootNamespace>InstructorsListApp</RootNamespace>
</PropertyGroup>
<ItemGroup>