forked from blogifierdotnet/Blogifier
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
hros18
committed
May 2, 2021
1 parent
a019c1d
commit 5ab4a8a
Showing
2 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System.Collections.ObjectModel; | ||
using Blogifier.Core.Data; | ||
using Blogifier.Shared; | ||
using Microsoft.EntityFrameworkCore; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Blogifier.Core.Providers | ||
{ | ||
|
||
public interface IConfigurationProvider { | ||
Task<Configuration> GetConfiguration(string key); | ||
Task<List<Configuration>> GetAllConfigurations(); | ||
|
||
Task<Configuration> Add(string key, bool value); | ||
Task<bool> Update(string key, bool value); | ||
} | ||
public class ConfigurationProvider : IConfigurationProvider | ||
{ | ||
private readonly AppDbContext _db; | ||
public ConfigurationProvider(AppDbContext db) | ||
{ | ||
_db = db; | ||
} | ||
public async Task<Configuration> Add(string key, bool value) | ||
{ | ||
var entity = await _db.Configurations.Where(c => c.Name == key).FirstOrDefaultAsync(); | ||
if (entity != null) { | ||
entity = new Configuration(){ Name = key, Active = value}; | ||
await _db.Configurations.AddAsync(entity); | ||
await _db.SaveChangesAsync(); | ||
} | ||
return entity; | ||
} | ||
|
||
public async Task<List<Configuration>> GetAllConfigurations() | ||
{ | ||
return await _db.Configurations.ToListAsync(); | ||
} | ||
|
||
public async Task<Configuration> GetConfiguration(string key) | ||
{ | ||
return await _db.Configurations.Where(c => c.Name == key).FirstOrDefaultAsync(); | ||
} | ||
|
||
public async Task<bool> Update(string key, bool value) | ||
{ | ||
var entity = await _db.Configurations.Where(c => c.Name == key).FirstOrDefaultAsync(); | ||
if (entity != null) { | ||
entity.Active = value; | ||
_db.Configurations.Update(entity); | ||
await _db.SaveChangesAsync(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using Blogifier.Core.Providers; | ||
using Blogifier.Shared; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.Collections.Generic; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
|
||
namespace Blogifier.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class ConfigurationController : ControllerBase | ||
{ | ||
private readonly IConfigurationProvider configurationProvider; | ||
|
||
public ConfigurationController(IConfigurationProvider provider) { | ||
configurationProvider = provider; | ||
} | ||
|
||
[HttpGet("{key}")] | ||
public async Task<IActionResult> GetByKey(string key) { | ||
try { | ||
var resp = await configurationProvider.GetConfiguration(key); | ||
return Ok(resp); | ||
} catch(Exception) { | ||
return BadRequest(); | ||
} | ||
} | ||
|
||
[HttpGet] | ||
public async Task<IActionResult> GetConfigurations() { | ||
try { | ||
var resp = await configurationProvider.GetAllConfigurations(); | ||
return Ok(resp); | ||
} catch(Exception) { | ||
return BadRequest(); | ||
} | ||
} | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> Create([FromBody] Configuration request) { | ||
try { | ||
var resp = await configurationProvider.Add(request.Name, request.Active); | ||
return Ok(resp); | ||
} catch(Exception) { | ||
return BadRequest(); | ||
} | ||
} | ||
|
||
[HttpPut("{key}")] | ||
public async Task<IActionResult> Update(Configuration request, string key) { | ||
try { | ||
var resp = await configurationProvider.Update(key, request.Active); | ||
return Ok(resp); | ||
} catch(Exception) { | ||
return BadRequest(); | ||
} | ||
} | ||
} | ||
} |