Merge branch 'feat/backend'

This commit is contained in:
ChronosX88 2019-09-25 18:41:10 +04:00
commit 75bf7d138f
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
9 changed files with 107 additions and 57 deletions

View File

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using InstructorsListApp.Models;
namespace InstructorsListApp.Controllers
{
[Route("api/v1/instructors")]
[ApiController]
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.SaveChanges();
return Ok(instructor.Id);
}
return BadRequest(ModelState);
}
[HttpPut("{id}")]
public IActionResult UpdateInstructorEntry(string id, [FromBody] Instructor instructor) {
if(ModelState.IsValid) {
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.Instructors.Update(instructor);
databaseContext.SaveChanges();
return Ok();
}
return NotFound("Given instructor isn't found");
}
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.SaveChanges();
return Ok();
}
return NotFound("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<DatabaseContext> 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,12 +21,19 @@ 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));
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@ -43,17 +50,17 @@ namespace instructors_crud_test_task
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
template: "{controller=Home}/{action=Index}/{id?}");
});
//app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSpaStaticFiles();
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=localhost;Database=instructorsDB;User Id=instructorsOwner;Password=P@ssW0rd;MultipleActiveResultSets=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>