mirror of
https://github.com/ChronosX88/InstructorsListAppTA.git
synced 2024-12-04 14:42:18 +00:00
[backend] feat: Implement basic CRUD operations for instructors management
This commit is contained in:
parent
670c9a0b9c
commit
3fe1be98d8
58
Controllers/InstructorsDataController.cs
Normal file
58
Controllers/InstructorsDataController.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
@ -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
12
Models/DatabaseContext.cs
Normal 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
8
Models/Instructor.cs
Normal 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;}
|
||||
}
|
||||
}
|
@ -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
|
||||
{
|
||||
|
@ -15,7 +15,7 @@
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"instructors_crud_test_task": {
|
||||
"InstructorsListApp": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"applicationUrl": "http://localhost:5000",
|
||||
|
21
Startup.cs
21
Startup.cs
@ -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,
|
||||
|
@ -4,5 +4,8 @@
|
||||
"Default": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
"AllowedHosts": "*",
|
||||
"ConnectionStrings": {
|
||||
"DefaultDatabase": "Server=(localdb)\\mssqllocaldb;Database=instructorsdb;Trusted_Connection=True;"
|
||||
}
|
||||
}
|
||||
|
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user