Skip to content

Commit

Permalink
Add providers
Browse files Browse the repository at this point in the history
  • Loading branch information
hros18 committed May 2, 2021
1 parent a019c1d commit 5ab4a8a
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/Blogifier.Core/Providers/ConfigurationProvider.cs
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;
}
}
}
64 changes: 64 additions & 0 deletions src/Blogifier/Controllers/ConfigurationController.cs
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();
}
}
}
}

0 comments on commit 5ab4a8a

Please sign in to comment.