add logging to SongController
This commit is contained in:
@@ -147,6 +147,7 @@
|
||||
</Compile>
|
||||
<Compile Include="Migrations\Configuration.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Services\Logger.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="favicon.ico" />
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
using API.Database;
|
||||
using API.Services;
|
||||
using Autofac;
|
||||
using Autofac.Integration.WebApi;
|
||||
using ProductService.Controllers;
|
||||
using System.Reflection;
|
||||
using System.Web.Http;
|
||||
|
||||
namespace API.App_Start {
|
||||
@@ -28,7 +28,7 @@ namespace API.App_Start {
|
||||
}
|
||||
|
||||
private static void RegisterServices(this ContainerBuilder builder) {
|
||||
// ... builder.RegisterType<object>();
|
||||
builder.RegisterType<Logger>();
|
||||
}
|
||||
|
||||
private static void RegisterDataProvider(this ContainerBuilder builder) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using API.Database;
|
||||
using API.Models;
|
||||
using API.Services;
|
||||
using Microsoft.AspNet.OData;
|
||||
using System.Data.Entity;
|
||||
using System.Data.Entity.Infrastructure;
|
||||
@@ -10,28 +11,37 @@ using System.Web.Http;
|
||||
namespace ProductService.Controllers {
|
||||
public class SongsController : ODataController {
|
||||
|
||||
public SongsController(DataContext db) {
|
||||
public SongsController(
|
||||
DataContext db,
|
||||
Logger logger
|
||||
) {
|
||||
this.db = db;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
private readonly DataContext db;
|
||||
private readonly Logger logger;
|
||||
|
||||
private bool ProductExists(int key) {
|
||||
logger.Log($"{nameof(SongsController)}.{nameof(ProductExists)}({key})");
|
||||
return db.Songs.Any(p => p.ID == key);
|
||||
}
|
||||
|
||||
[EnableQuery]
|
||||
public IQueryable<Song> Get() {
|
||||
logger.Log($"{nameof(SongsController)}.{nameof(Get)}()");
|
||||
return db.Songs;
|
||||
}
|
||||
|
||||
[EnableQuery]
|
||||
public SingleResult<Song> Get([FromODataUri] int key) {
|
||||
logger.Log($"{nameof(SongsController)}.{nameof(Get)}({key})");
|
||||
var result = db.Songs.Where(p => p.ID == key);
|
||||
return SingleResult.Create(result);
|
||||
}
|
||||
|
||||
public async Task<IHttpActionResult> Post(Song song) {
|
||||
logger.Log($"{nameof(SongsController)}.{nameof(Post)}()");
|
||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||
|
||||
db.Songs.Add(song);
|
||||
@@ -40,6 +50,7 @@ namespace ProductService.Controllers {
|
||||
}
|
||||
|
||||
public async Task<IHttpActionResult> Patch([FromODataUri] int key, Delta<Song> product) {
|
||||
logger.Log($"{nameof(SongsController)}.{nameof(Patch)}({key})");
|
||||
if (!ModelState.IsValid) {
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
@@ -59,6 +70,7 @@ namespace ProductService.Controllers {
|
||||
}
|
||||
|
||||
public async Task<IHttpActionResult> Put([FromODataUri] int key, Song update) {
|
||||
logger.Log($"{nameof(SongsController)}.{nameof(Put)}({key})");
|
||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||
if (key != update.ID) return BadRequest();
|
||||
|
||||
@@ -75,6 +87,7 @@ namespace ProductService.Controllers {
|
||||
}
|
||||
|
||||
public async Task<IHttpActionResult> Delete([FromODataUri] int key) {
|
||||
logger.Log($"{nameof(SongsController)}.{nameof(Delete)}({key})");
|
||||
var product = await db.Songs.FindAsync(key);
|
||||
if (product == null) {
|
||||
return NotFound();
|
||||
|
||||
22
API/API/Services/Logger.cs
Normal file
22
API/API/Services/Logger.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using API.Database;
|
||||
using API.Models;
|
||||
using System;
|
||||
|
||||
namespace API.Services {
|
||||
public class Logger {
|
||||
private readonly DataContext db;
|
||||
|
||||
public Logger(DataContext db) {
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
public void Log(string method) {
|
||||
var log = new Log {
|
||||
Date = DateTime.Now,
|
||||
Method = method
|
||||
};
|
||||
db.Logs.Add(log);
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user