Skip to content

Commit 2372bc2

Browse files
committed
simplcommerce#664 standardize apis: appsetting and brands
1 parent 1e6c76d commit 2372bc2

9 files changed

Lines changed: 79 additions & 44 deletions

File tree

src/Modules/SimplCommerce.Module.Catalog/Areas/Catalog/Controllers/BrandApiController.cs

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System.Collections.Generic;
2+
using System.Linq;
23
using System.Threading.Tasks;
34
using Microsoft.AspNetCore.Authorization;
45
using Microsoft.AspNetCore.Mvc;
@@ -13,7 +14,8 @@ namespace SimplCommerce.Module.Catalog.Areas.Catalog.Controllers
1314
[Area("Catalog")]
1415
[Authorize(Roles = "admin, vendor")]
1516
[Route("api/brands")]
16-
public class BrandApiController : Controller
17+
[ApiController]
18+
public class BrandApiController : ControllerBase
1719
{
1820
private readonly IRepository<Brand> _brandRepository;
1921
private readonly IBrandService _brandService;
@@ -25,68 +27,72 @@ public BrandApiController(IRepository<Brand> brandRepository, IBrandService bran
2527
}
2628

2729
[HttpGet]
28-
public async Task<IActionResult> Get()
30+
public async Task<ActionResult<IList<BrandVm>>> Get()
2931
{
30-
var brandList = await _brandRepository.Query().Where(x => !x.IsDeleted).ToListAsync();
32+
var brands = await _brandRepository.Query()
33+
.Where(x => !x.IsDeleted)
34+
.Select(x => new BrandVm
35+
{
36+
Id = x.Id,
37+
Name = x.Name,
38+
Slug = x.Slug,
39+
IsPublished = x.IsPublished
40+
}).ToListAsync();
3141

32-
return Json(brandList);
42+
return brands;
3343
}
3444

3545
[HttpGet("{id}")]
36-
public async Task<IActionResult> Get(long id)
46+
public async Task<ActionResult<BrandVm>> Get(long id)
3747
{
3848
var brand = await _brandRepository.Query().FirstOrDefaultAsync(x => x.Id == id);
39-
var model = new BrandForm
49+
if(brand == null)
50+
{
51+
return NotFound();
52+
}
53+
54+
var model = new BrandVm
4055
{
4156
Id = brand.Id,
4257
Name = brand.Name,
4358
Slug = brand.Slug,
4459
IsPublished = brand.IsPublished
4560
};
4661

47-
return Json(model);
62+
return model;
4863
}
4964

5065
[HttpPost]
5166
[Authorize(Roles = "admin")]
5267
public async Task<IActionResult> Post([FromBody] BrandForm model)
5368
{
54-
if (ModelState.IsValid)
69+
var brand = new Brand
5570
{
56-
var brand = new Brand
57-
{
58-
Name = model.Name,
59-
Slug = model.Slug,
60-
IsPublished = model.IsPublished
61-
};
71+
Name = model.Name,
72+
Slug = model.Slug,
73+
IsPublished = model.IsPublished
74+
};
6275

63-
await _brandService.Create(brand);
64-
return CreatedAtAction(nameof(Get), new { id = brand.Id }, null);
65-
}
66-
return BadRequest(ModelState);
76+
await _brandService.Create(brand);
77+
return CreatedAtAction(nameof(Get), new { id = brand.Id }, null);
6778
}
6879

6980
[HttpPut("{id}")]
7081
[Authorize(Roles = "admin")]
7182
public async Task<IActionResult> Put(long id, [FromBody] BrandForm model)
7283
{
73-
if (ModelState.IsValid)
84+
var brand = _brandRepository.Query().FirstOrDefault(x => x.Id == id);
85+
if(brand == null)
7486
{
75-
var brand = _brandRepository.Query().FirstOrDefault(x => x.Id == id);
76-
if(brand == null)
77-
{
78-
return NotFound();
79-
}
80-
81-
brand.Name = model.Name;
82-
brand.Slug = model.Slug;
83-
brand.IsPublished = model.IsPublished;
84-
85-
await _brandService.Update(brand);
86-
return Accepted();
87+
return NotFound();
8788
}
8889

89-
return BadRequest(ModelState);
90+
brand.Name = model.Name;
91+
brand.Slug = model.Slug;
92+
brand.IsPublished = model.IsPublished;
93+
94+
await _brandService.Update(brand);
95+
return Accepted();
9096
}
9197

9298
[HttpDelete("{id}")]

src/Modules/SimplCommerce.Module.Catalog/Areas/Catalog/ViewModels/BrandForm.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ public BrandForm()
99
IsPublished = true;
1010
}
1111

12-
public long Id { get; set; }
13-
1412
[Required(ErrorMessage = "The {0} field is required.")]
1513
public string Slug { get; set; }
1614

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace SimplCommerce.Module.Catalog.Areas.Catalog.ViewModels
2+
{
3+
public class BrandVm : BrandForm
4+
{
5+
public long Id { get; set; }
6+
}
7+
}

src/Modules/SimplCommerce.Module.Catalog/wwwroot/admin/brand/brand-form.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ <h2 ng-if="vm.isEditMode">{{::vm.translate.get('Edit Brand')}}</h2>
2222
<input name="slug" ng-model="vm.brand.slug" class="form-control" />
2323
</div>
2424
</div>
25+
<div class="form-group">
26+
<div class="col-sm-offset-2 col-sm-10">
27+
<div class="checkbox">
28+
<label>
29+
<input type="checkbox" ng-model="vm.brand.isPublished">{{::vm.translate.get('Is Published')}}
30+
</label>
31+
</div>
32+
</div>
33+
</div>
2534
<div class="form-group">
2635
<div class="col-sm-offset-2 col-sm-10">
2736
<button class="btn btn-primary" ng-click="vm.save()"><span class="glyphicon glyphicon-ok"></span> {{::vm.translate.get('Save')}}</button>
@@ -30,4 +39,4 @@ <h2 ng-if="vm.isEditMode">{{::vm.translate.get('Edit Brand')}}</h2>
3039
</div>
3140
</form>
3241
</div>
33-
</div>
42+
</div>

src/Modules/SimplCommerce.Module.Catalog/wwwroot/admin/brand/brand-form.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
function BrandFormCtrl($state, $stateParams, brandService, translateService) {
88
var vm = this;
99
vm.translate = translateService;
10-
vm.brand = {};
10+
vm.brand = { isPublished: true };
1111
vm.brandId = $stateParams.id;
1212
vm.isEditMode = vm.brandId > 0;
1313

src/Modules/SimplCommerce.Module.Core/Areas/Core/Controllers/AppSettingApiController.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
using Microsoft.EntityFrameworkCore;
77
using Microsoft.Extensions.Configuration;
88
using SimplCommerce.Infrastructure.Data;
9+
using SimplCommerce.Module.Core.Areas.Core.ViewModels;
910
using SimplCommerce.Module.Core.Models;
1011

1112
namespace SimplCommerce.Module.Core.Areas.Core.Controllers
1213
{
1314
[Area("Core")]
1415
[Authorize(Roles = "admin")]
1516
[Route("api/appsettings")]
16-
public class AppSettingApiController : Controller
17+
[ApiController]
18+
public class AppSettingApiController : ControllerBase
1719
{
1820
private readonly IRepositoryWithTypedId<AppSetting, string> _appSettingRepository;
1921
private readonly IConfigurationRoot _configurationRoot;
@@ -25,21 +27,24 @@ public AppSettingApiController(IRepositoryWithTypedId<AppSetting, string> appSet
2527
}
2628

2729
[HttpGet]
28-
public async Task<IActionResult> Get()
30+
public async Task<ActionResult<IList<AppSettingVm>>> Get()
2931
{
30-
var settings = await _appSettingRepository.Query().Where(x => x.IsVisibleInCommonSettingPage).ToListAsync();
31-
return Json(settings);
32+
var settings = await _appSettingRepository.Query()
33+
.Where(x => x.IsVisibleInCommonSettingPage)
34+
.Select(x => new AppSettingVm { Key = x.Id, Value = x.Value })
35+
.ToListAsync();
36+
return settings;
3237
}
3338

3439
[HttpPut]
35-
public async Task<IActionResult> Put([FromBody] IList<AppSetting> model)
40+
public async Task<IActionResult> Put([FromBody] IList<AppSettingVm> model)
3641
{
3742
if (ModelState.IsValid)
3843
{
3944
var settings = await _appSettingRepository.Query().Where(x => x.IsVisibleInCommonSettingPage).ToListAsync();
4045
foreach(var item in settings)
4146
{
42-
var vm = model.FirstOrDefault(x => x.Id == item.Id);
47+
var vm = model.FirstOrDefault(x => x.Key == item.Id);
4348
if (vm != null)
4449
{
4550
item.Value = vm.Value;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace SimplCommerce.Module.Core.Areas.Core.ViewModels
2+
{
3+
public class AppSettingVm
4+
{
5+
public string Key { get; set; }
6+
7+
public string Value { get; set; }
8+
}
9+
}

src/Modules/SimplCommerce.Module.Core/wwwroot/admin/configuration/configuration.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ <h2>{{::vm.translate.get('Application Settings')}}</h2>
1010
</ul>
1111
</div>
1212
<div class="form-group" ng-repeat="item in vm.settings">
13-
<label class="col-sm-4 control-label" title="{{item.id}}">{{::vm.translate.get(item.id)}}</label>
13+
<label class="col-sm-4 control-label" title="{{item.key}}">{{::vm.translate.get(item.key)}}</label>
1414
<div class="col-sm-8">
1515
<input name="name" ng-model="item.value" class="form-control"/>
1616
</div>

src/Modules/SimplCommerce.Module.PaymentBraintree/Areas/PaymentBraintree/Controllers/BraintreeApiController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace SimplCommerce.Module.PaymentBraintree.Areas.PaymentBraintree.Controlle
1313
[Area("PaymentBraintree")]
1414
[Authorize(Roles = "admin")]
1515
[Route("api/braintree")]
16+
[ApiExplorerSettings(IgnoreApi = true)]
1617
public class BraintreeApiController : Controller
1718
{
1819
private readonly IRepositoryWithTypedId<PaymentProvider, string> _paymentProviderRepository;

0 commit comments

Comments
 (0)