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