Skip to content

Commit

Permalink
simplcommerce#664 refactor carousel widget apis
Browse files Browse the repository at this point in the history
  • Loading branch information
thiennn committed Nov 16, 2019
1 parent 5f06ade commit a946ec5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
var error = response.data;
vm.validationErrors = [];
if (error && angular.isObject(error)) {
for (var key in error) {
vm.validationErrors.push(error[key][0]);
var errors = error.errors ? error.errors : error;
for (var key in errors) {
vm.validationErrors.push(errors[key][0]);
}
} else {
vm.validationErrors.push('Could not add brand.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ namespace SimplCommerce.Module.Cms.Areas.Cms.Controllers
[Area("Cms")]
[Authorize(Roles = "admin")]
[Route("api/carousel-widgets")]
public class CarouselWidgetApiController : Controller
[ApiController]
public class CarouselWidgetApiController : ControllerBase
{
private readonly IRepository<WidgetInstance> _widgetInstanceRepository;
private readonly IMediaService _mediaService;
Expand All @@ -31,7 +32,7 @@ public CarouselWidgetApiController(IRepository<WidgetInstance> widgetInstanceRep
}

[HttpGet("{id}")]
public async Task<IActionResult> Get(long id)
public async Task<ActionResult<CarouselWidgetForm>> Get(long id)
{
var totalWidgets = _widgetInstanceRepository.Query().ToList().Count();
var widgetInstance = await _widgetInstanceRepository.Query().FirstOrDefaultAsync(x => x.Id == id);
Expand All @@ -51,43 +52,45 @@ public async Task<IActionResult> Get(long id)
item.ImageUrl = _mediaService.GetMediaUrl(item.Image);
}

return Json(model);
return model;
}

[HttpPost]
public async Task<IActionResult> Post(IFormCollection formCollection)
public async Task<IActionResult> Post([FromForm]CarouselWidgetForm model)
{
var model = ToCarouselWidgetFormModel(formCollection);
if (ModelState.IsValid)
ModelBindUploadFiles(model);

if(model.Items.Any(x => x.UploadImage == null))
{
foreach (var item in model.Items)
{
item.Image = await SaveFile(item.UploadImage);
}
ModelState.AddModelError("Images", "Images is required");
return BadRequest(ModelState);
}

var widgetInstance = new WidgetInstance
{
Name = model.Name,
WidgetId = "CarouselWidget",
WidgetZoneId = model.WidgetZoneId,
PublishStart = model.PublishStart,
PublishEnd = model.PublishEnd,
DisplayOrder = model.DisplayOrder,
Data = JsonConvert.SerializeObject(model.Items)
};

_widgetInstanceRepository.Add(widgetInstance);
await _widgetInstanceRepository.SaveChangesAsync();
return CreatedAtAction(nameof(Get), new { id = widgetInstance.Id }, null);
foreach (var item in model.Items)
{
item.Image = await SaveFile(item.UploadImage);
}

return BadRequest(ModelState);
var widgetInstance = new WidgetInstance
{
Name = model.Name,
WidgetId = "CarouselWidget",
WidgetZoneId = model.WidgetZoneId,
PublishStart = model.PublishStart,
PublishEnd = model.PublishEnd,
DisplayOrder = model.DisplayOrder,
Data = JsonConvert.SerializeObject(model.Items)
};

_widgetInstanceRepository.Add(widgetInstance);
await _widgetInstanceRepository.SaveChangesAsync();
return CreatedAtAction(nameof(Get), new { id = widgetInstance.Id }, null);
}

[HttpPut("{id}")]
public async Task<IActionResult> Put(long id, IFormCollection formCollection)
public async Task<IActionResult> Put(long id, [FromForm]CarouselWidgetForm model)
{
var model = ToCarouselWidgetFormModel(formCollection);
ModelBindUploadFiles(model);

foreach (var item in model.Items)
{
Expand All @@ -101,56 +104,28 @@ public async Task<IActionResult> Put(long id, IFormCollection formCollection)
}
}

if (ModelState.IsValid)
var widgetInstance = await _widgetInstanceRepository.Query().FirstOrDefaultAsync(x => x.Id == id);
if(widgetInstance == null)
{
var widgetInstance = await _widgetInstanceRepository.Query().FirstOrDefaultAsync(x => x.Id == id);
if(widgetInstance == null)
{
return NotFound();
}

widgetInstance.Name = model.Name;
widgetInstance.PublishStart = model.PublishStart;
widgetInstance.PublishEnd = model.PublishEnd;
widgetInstance.WidgetZoneId = model.WidgetZoneId;
widgetInstance.DisplayOrder = model.DisplayOrder;
widgetInstance.Data = JsonConvert.SerializeObject(model.Items);

await _widgetInstanceRepository.SaveChangesAsync();
return Accepted();
return NotFound();
}

return BadRequest(ModelState);
widgetInstance.Name = model.Name;
widgetInstance.PublishStart = model.PublishStart;
widgetInstance.PublishEnd = model.PublishEnd;
widgetInstance.WidgetZoneId = model.WidgetZoneId;
widgetInstance.DisplayOrder = model.DisplayOrder;
widgetInstance.Data = JsonConvert.SerializeObject(model.Items);

await _widgetInstanceRepository.SaveChangesAsync();
return Accepted();
}

private CarouselWidgetForm ToCarouselWidgetFormModel(IFormCollection formCollection)
private CarouselWidgetForm ModelBindUploadFiles(CarouselWidgetForm model)
{
var model = new CarouselWidgetForm();
model.Name = formCollection["name"];
model.WidgetZoneId = int.Parse(formCollection["widgetZoneId"]);
int.TryParse(formCollection["displayOrder"], out int displayOrder);
model.DisplayOrder = displayOrder;
if (DateTimeOffset.TryParse(formCollection["publishStart"], out DateTimeOffset publishStart))
{
model.PublishStart = publishStart;
}

if(DateTimeOffset.TryParse(formCollection["publishEnd"], out DateTimeOffset publishEnd))
{
model.PublishEnd = publishEnd;
}

int numberOfItems = int.Parse(formCollection["numberOfItems"]);
for (var i = 0; i < numberOfItems; i++)
for (var i = 0; i < model.Items.Count; i++)
{
var item = new CarouselWidgetItemForm();
item.Caption = formCollection[$"items[{i}][caption]"];
item.SubCaption = formCollection[$"items[{i}][subCaption]"];
item.TargetUrl = formCollection[$"items[{i}][targetUrl]"];
item.LinkText = formCollection[$"items[{i}][linkText]"];
item.Image = formCollection[$"items[{i}][image]"];
item.UploadImage = formCollection.Files[$"items[{i}][uploadImage]"];
model.Items.Add(item);
model.Items[i].UploadImage = Request.Form.Files[$"items[{i}][uploadImage]"];
}

return model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace SimplCommerce.Module.Cms.Areas.Cms.ViewModels
{
public class CarouselWidgetForm : WidgetFormBase
{
public IList<CarouselWidgetItemForm> Items = new List<CarouselWidgetItemForm>();
public IList<CarouselWidgetItemForm> Items { get; set; } = new List<CarouselWidgetItemForm>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
var promise;

// ng-upload will post null as text
vm.widgetInstance.publishEnd = vm.widgetInstance.publishEnd === null ? '' : vm.widgetInstance.publishEnd;
angular.forEach(vm.widgetInstance.items, function (item) {
item.caption = item.caption === null ? '' : item.caption;
item.subCaption = item.subCaption === null ? '' : item.subCaption;
Expand All @@ -53,8 +54,9 @@
var error = response.data;
vm.validationErrors = [];
if (error && angular.isObject(error)) {
for (var key in error) {
vm.validationErrors.push(error[key][0]);
var errors = error.errors ? error.errors : error;
for (var key in errors) {
vm.validationErrors.push(errors[key][0]);
}
} else {
vm.validationErrors.push('Could not carousel widget.');
Expand Down

0 comments on commit a946ec5

Please sign in to comment.