Skip to content

Commit

Permalink
simplcommerce#664 standardize apis: appsetting and brands
Browse files Browse the repository at this point in the history
  • Loading branch information
bruslega committed Nov 16, 2019
1 parent e98f01d commit e4c3797
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -13,7 +14,8 @@ namespace SimplCommerce.Module.Catalog.Areas.Catalog.Controllers
[Area("Catalog")]
[Authorize(Roles = "admin, vendor")]
[Route("api/brands")]
public class BrandApiController : Controller
[ApiController]
public class BrandApiController : ControllerBase
{
private readonly IRepository<Brand> _brandRepository;
private readonly IBrandService _brandService;
Expand All @@ -25,68 +27,72 @@ public BrandApiController(IRepository<Brand> brandRepository, IBrandService bran
}

[HttpGet]
public async Task<IActionResult> Get()
public async Task<ActionResult<IList<BrandVm>>> Get()
{
var brandList = await _brandRepository.Query().Where(x => !x.IsDeleted).ToListAsync();
var brands = await _brandRepository.Query()
.Where(x => !x.IsDeleted)
.Select(x => new BrandVm
{
Id = x.Id,
Name = x.Name,
Slug = x.Slug,
IsPublished = x.IsPublished
}).ToListAsync();

return Json(brandList);
return brands;
}

[HttpGet("{id}")]
public async Task<IActionResult> Get(long id)
public async Task<ActionResult<BrandVm>> Get(long id)
{
var brand = await _brandRepository.Query().FirstOrDefaultAsync(x => x.Id == id);
var model = new BrandForm
if(brand == null)
{
return NotFound();
}

var model = new BrandVm
{
Id = brand.Id,
Name = brand.Name,
Slug = brand.Slug,
IsPublished = brand.IsPublished
};

return Json(model);
return model;
}

[HttpPost]
[Authorize(Roles = "admin")]
public async Task<IActionResult> Post([FromBody] BrandForm model)
{
if (ModelState.IsValid)
var brand = new Brand
{
var brand = new Brand
{
Name = model.Name,
Slug = model.Slug,
IsPublished = model.IsPublished
};
Name = model.Name,
Slug = model.Slug,
IsPublished = model.IsPublished
};

await _brandService.Create(brand);
return CreatedAtAction(nameof(Get), new { id = brand.Id }, null);
}
return BadRequest(ModelState);
await _brandService.Create(brand);
return CreatedAtAction(nameof(Get), new { id = brand.Id }, null);
}

[HttpPut("{id}")]
[Authorize(Roles = "admin")]
public async Task<IActionResult> Put(long id, [FromBody] BrandForm model)
{
if (ModelState.IsValid)
var brand = _brandRepository.Query().FirstOrDefault(x => x.Id == id);
if(brand == null)
{
var brand = _brandRepository.Query().FirstOrDefault(x => x.Id == id);
if(brand == null)
{
return NotFound();
}

brand.Name = model.Name;
brand.Slug = model.Slug;
brand.IsPublished = model.IsPublished;

await _brandService.Update(brand);
return Accepted();
return NotFound();
}

return BadRequest(ModelState);
brand.Name = model.Name;
brand.Slug = model.Slug;
brand.IsPublished = model.IsPublished;

await _brandService.Update(brand);
return Accepted();
}

[HttpDelete("{id}")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ public BrandForm()
IsPublished = true;
}

public long Id { get; set; }

[Required(ErrorMessage = "The {0} field is required.")]
public string Slug { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SimplCommerce.Module.Catalog.Areas.Catalog.ViewModels
{
public class BrandVm : BrandForm
{
public long Id { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ <h2 ng-if="vm.isEditMode">{{::vm.translate.get('Edit Brand')}}</h2>
<input name="slug" ng-model="vm.brand.slug" class="form-control" />
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox" ng-model="vm.brand.isPublished">{{::vm.translate.get('Is Published')}}
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button class="btn btn-primary" ng-click="vm.save()"><span class="glyphicon glyphicon-ok"></span> {{::vm.translate.get('Save')}}</button>
Expand All @@ -30,4 +39,4 @@ <h2 ng-if="vm.isEditMode">{{::vm.translate.get('Edit Brand')}}</h2>
</div>
</form>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
function BrandFormCtrl($state, $stateParams, brandService, translateService) {
var vm = this;
vm.translate = translateService;
vm.brand = {};
vm.brand = { isPublished: true };
vm.brandId = $stateParams.id;
vm.isEditMode = vm.brandId > 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using SimplCommerce.Infrastructure.Data;
using SimplCommerce.Module.Core.Areas.Core.ViewModels;
using SimplCommerce.Module.Core.Models;

namespace SimplCommerce.Module.Core.Areas.Core.Controllers
{
[Area("Core")]
[Authorize(Roles = "admin")]
[Route("api/appsettings")]
public class AppSettingApiController : Controller
[ApiController]
public class AppSettingApiController : ControllerBase
{
private readonly IRepositoryWithTypedId<AppSetting, string> _appSettingRepository;
private readonly IConfigurationRoot _configurationRoot;
Expand All @@ -25,21 +27,24 @@ public AppSettingApiController(IRepositoryWithTypedId<AppSetting, string> appSet
}

[HttpGet]
public async Task<IActionResult> Get()
public async Task<ActionResult<IList<AppSettingVm>>> Get()
{
var settings = await _appSettingRepository.Query().Where(x => x.IsVisibleInCommonSettingPage).ToListAsync();
return Json(settings);
var settings = await _appSettingRepository.Query()
.Where(x => x.IsVisibleInCommonSettingPage)
.Select(x => new AppSettingVm { Key = x.Id, Value = x.Value })
.ToListAsync();
return settings;
}

[HttpPut]
public async Task<IActionResult> Put([FromBody] IList<AppSetting> model)
public async Task<IActionResult> Put([FromBody] IList<AppSettingVm> model)
{
if (ModelState.IsValid)
{
var settings = await _appSettingRepository.Query().Where(x => x.IsVisibleInCommonSettingPage).ToListAsync();
foreach(var item in settings)
{
var vm = model.FirstOrDefault(x => x.Id == item.Id);
var vm = model.FirstOrDefault(x => x.Key == item.Id);
if (vm != null)
{
item.Value = vm.Value;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace SimplCommerce.Module.Core.Areas.Core.ViewModels
{
public class AppSettingVm
{
public string Key { get; set; }

public string Value { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ <h2>{{::vm.translate.get('Application Settings')}}</h2>
</ul>
</div>
<div class="form-group" ng-repeat="item in vm.settings">
<label class="col-sm-4 control-label" title="{{item.id}}">{{::vm.translate.get(item.id)}}</label>
<label class="col-sm-4 control-label" title="{{item.key}}">{{::vm.translate.get(item.key)}}</label>
<div class="col-sm-8">
<input name="name" ng-model="item.value" class="form-control"/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace SimplCommerce.Module.PaymentBraintree.Areas.PaymentBraintree.Controlle
[Area("PaymentBraintree")]
[Authorize(Roles = "admin")]
[Route("api/braintree")]
[ApiExplorerSettings(IgnoreApi = true)]
public class BraintreeApiController : Controller
{
private readonly IRepositoryWithTypedId<PaymentProvider, string> _paymentProviderRepository;
Expand Down

0 comments on commit e4c3797

Please sign in to comment.