Init
- aggiunto un po di tutto comeil progetto del prof
This commit is contained in:
24
SeniorAssistant/Controllers/BaseController.cs
Normal file
24
SeniorAssistant/Controllers/BaseController.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SeniorAssistant.Data;
|
||||
|
||||
namespace SeniorAssistant.Controllers
|
||||
{
|
||||
public abstract class BaseController : Controller
|
||||
{
|
||||
IDataContextFactory<SeniorDataContext> dbFactory;
|
||||
SeniorDataContext db;
|
||||
|
||||
protected T TryResolve<T>() => (T)HttpContext.RequestServices.GetService(typeof(T));
|
||||
|
||||
protected IDataContextFactory<SeniorDataContext> DbFactory => dbFactory ?? (dbFactory = TryResolve<IDataContextFactory<SeniorDataContext>>());
|
||||
|
||||
protected SeniorDataContext Db => db ?? (db = DbFactory.Create());
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
db?.Dispose();
|
||||
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
}
|
||||
}
|
||||
21
SeniorAssistant/Controllers/HomeController.cs
Normal file
21
SeniorAssistant/Controllers/HomeController.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace SeniorAssistant.Controllers
|
||||
{
|
||||
public class HomeController : Controller
|
||||
{
|
||||
[Route("")]
|
||||
[Route("Home")]
|
||||
[Route("Index")]
|
||||
public IActionResult Index()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
[Route("Heartbeat")]
|
||||
public IActionResult Heartbeat()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
}
|
||||
}
|
||||
33
SeniorAssistant/Controllers/Services/CrudController.cs
Normal file
33
SeniorAssistant/Controllers/Services/CrudController.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using LinqToDB;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SeniorAssistant.Models;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SeniorAssistant.Controllers.Services
|
||||
{
|
||||
public abstract class CrudController<TEntity> : BaseController
|
||||
where TEntity : class, IHasUsername
|
||||
{
|
||||
[HttpGet]
|
||||
public async Task<IEnumerable<TEntity>> Read() => await Db.GetTable<TEntity>().ToListAsync();
|
||||
|
||||
[HttpGet("{username}")]
|
||||
public async Task<TEntity> Read(string username) => await Db.GetTable<TEntity>().FirstOrDefaultAsync(c => c.Username.Equals(username));
|
||||
|
||||
[HttpPost]
|
||||
public async Task Create([FromBody]TEntity item) => await Db.InsertAsync(item);
|
||||
|
||||
[HttpPut("{username}")]
|
||||
public async Task Update(string username, [FromBody]TEntity item)
|
||||
{
|
||||
item.Username = username;
|
||||
|
||||
await Db.UpdateAsync(item);
|
||||
}
|
||||
|
||||
[HttpDelete("{username}")]
|
||||
public async Task Delete(string username) => await Db.GetTable<TEntity>().Where(c => c.Username.Equals(username)).DeleteAsync();
|
||||
}
|
||||
}
|
||||
50
SeniorAssistant/Controllers/Services/CrudTimeController.cs
Normal file
50
SeniorAssistant/Controllers/Services/CrudTimeController.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using LinqToDB;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SeniorAssistant.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SeniorAssistant.Controllers.Services
|
||||
{
|
||||
public class CrudTimeController<TEntity> : BaseController
|
||||
where TEntity : class, IHasTime
|
||||
{
|
||||
[HttpGet]
|
||||
public async Task<IEnumerable<TEntity>> Read() => await Db.GetTable<TEntity>().ToListAsync();
|
||||
|
||||
[HttpGet("{username}")]
|
||||
public async Task<IEnumerable<TEntity>> Read(string username) => await Db.GetTable<TEntity>().Where(e => e.Username.Equals(username)).ToListAsync();
|
||||
|
||||
[HttpGet("{username}/{date:regex((today|\\d{{4}}-\\d{{2}}-\\d{{2}}))}/{hour:range(0, 23)?}")]
|
||||
public async Task<IEnumerable<TEntity>> Read(string username, string date, int hour = -1)
|
||||
{
|
||||
DateTime time = (date.Equals("today") ? DateTime.Now : DateTime.ParseExact(date, "yyyy-MM-dd", null) );
|
||||
|
||||
return await Db.GetTable<TEntity>().Where(e =>
|
||||
e.Username.Equals(username) &&
|
||||
(time.Year == 0 || e.Time.Year == time.Year) &&
|
||||
(time.Month == 0 || e.Time.Month == time.Month) &&
|
||||
(time.Day == 0 || e.Time.Day == time.Day) &&
|
||||
(hour < 0 || e.Time.Hour == hour)
|
||||
).ToListAsync();
|
||||
}
|
||||
|
||||
/*
|
||||
[HttpPost("{username}")]
|
||||
public async Task Create([FromBody]TEntity item) => await Db.InsertAsync(item);
|
||||
|
||||
[HttpPut("{username}/{date}/{time}")]
|
||||
public async Task Update(string username, [FromBody]TEntity item)
|
||||
{
|
||||
item.Username = username;
|
||||
|
||||
await Db.UpdateAsync(item);
|
||||
}
|
||||
|
||||
[HttpDelete("{username}")]
|
||||
public async Task Delete(string username) => await Db.GetTable<TEntity>().Where(c => c.Username.Equals(username)).DeleteAsync();
|
||||
*/
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SeniorAssistant.Models;
|
||||
|
||||
namespace SeniorAssistant.Controllers.Services
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
public class HeartbeatController : CrudTimeController<Heartbeat>
|
||||
{ }
|
||||
}
|
||||
9
SeniorAssistant/Controllers/Services/UserController.cs
Normal file
9
SeniorAssistant/Controllers/Services/UserController.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SeniorAssistant.Models;
|
||||
|
||||
namespace SeniorAssistant.Controllers.Services
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
public class UserController : CrudController<User>
|
||||
{ }
|
||||
}
|
||||
Reference in New Issue
Block a user