diff --git a/src/Blogifier.Core/Providers/ConfigurationProvider.cs b/src/Blogifier.Core/Providers/ConfigurationProvider.cs new file mode 100755 index 000000000..edccf9093 --- /dev/null +++ b/src/Blogifier.Core/Providers/ConfigurationProvider.cs @@ -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 GetConfiguration(string key); + Task> GetAllConfigurations(); + + Task Add(string key, bool value); + Task Update(string key, bool value); + } + public class ConfigurationProvider : IConfigurationProvider + { + private readonly AppDbContext _db; + public ConfigurationProvider(AppDbContext db) + { + _db = db; + } + public async Task 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> GetAllConfigurations() + { + return await _db.Configurations.ToListAsync(); + } + + public async Task GetConfiguration(string key) + { + return await _db.Configurations.Where(c => c.Name == key).FirstOrDefaultAsync(); + } + + public async Task 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; + } + } +} diff --git a/src/Blogifier/Controllers/ConfigurationController.cs b/src/Blogifier/Controllers/ConfigurationController.cs new file mode 100755 index 000000000..5e4101e15 --- /dev/null +++ b/src/Blogifier/Controllers/ConfigurationController.cs @@ -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 GetByKey(string key) { + try { + var resp = await configurationProvider.GetConfiguration(key); + return Ok(resp); + } catch(Exception) { + return BadRequest(); + } + } + + [HttpGet] + public async Task GetConfigurations() { + try { + var resp = await configurationProvider.GetAllConfigurations(); + return Ok(resp); + } catch(Exception) { + return BadRequest(); + } + } + + [HttpPost] + public async Task 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 Update(Configuration request, string key) { + try { + var resp = await configurationProvider.Update(key, request.Active); + return Ok(resp); + } catch(Exception) { + return BadRequest(); + } + } + } +}