Skip to content

Commit

Permalink
Implement CRUD for Warehouses (simplcommerce#389)
Browse files Browse the repository at this point in the history
* Warehouse CRUD
  • Loading branch information
tdavisjr authored and thiennn committed Feb 5, 2018
1 parent 011524e commit f318435
Show file tree
Hide file tree
Showing 13 changed files with 615 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace SimplCommerce.Module.Core.Controllers
{
[Route("api/location")]
public class LocationController : Controller
{
private readonly IRepository<District> districtRepository;
Expand All @@ -29,5 +30,25 @@ public IActionResult GetDistricts(long stateOrProvinceId)

return Json(districts);
}

[Route("districts")]
public IActionResult Get(long? stateOrProvinceId)
{
var districts = districtRepository.Query();

if (stateOrProvinceId.HasValue)
{
districts = districts.Where(d => d.StateOrProvinceId == stateOrProvinceId.GetValueOrDefault());
}

if (!districts.Any())
{
return NotFound();
}

districts = districts.OrderBy(x => x.Name);

return Json(districts.Select(d=> new { d.Id, d.Name }).ToList());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,24 @@ public async Task<IActionResult> GetStatesOrProvinces(long countryId)
return Ok(statesOrProvinces);
}

public async Task<IActionResult> Get()
{
var statesOrProvinces = await _stateOrProvinceRepository.Query()
.OrderBy(x => x.Name)
.Select(x => new
{
x.Id,
x.Name
})
.ToListAsync();

return Ok(statesOrProvinces);
}

[HttpPost("grid")]
public IActionResult List(int countryId, [FromBody] SmartTableParam param)
{
var query = _stateOrProvinceRepository.Query().Where(sp=> sp.CountryId == countryId);
var query = _stateOrProvinceRepository.Query().Where(sp => sp.CountryId == countryId);

if (param.Search.PredicateObject != null)
{
Expand All @@ -58,13 +72,13 @@ public IActionResult List(int countryId, [FromBody] SmartTableParam param)

var stateProvinces = query.ToSmartTableResult(
param,
sp=> new
{
sp.Id,
sp.Name,
sp.Code,
sp.CountryCode
});
sp => new
{
sp.Id,
sp.Name,
sp.Code,
sp.CountryCode
});

return Json(stateProvinces);
}
Expand Down
3 changes: 3 additions & 0 deletions src/Modules/SimplCommerce.Module.Core/Models/Address.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using SimplCommerce.Infrastructure.Models;

namespace SimplCommerce.Module.Core.Models
Expand All @@ -21,10 +22,12 @@ public class Address : EntityBase

public District District { get; set; }

[Required]
public long StateOrProvinceId { get; set; }

public StateOrProvince StateOrProvince { get; set; }

[Required]
public long CountryId { get; set; }

public Country Country { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@
<script simpl-append-version="true" src="~/modules/inventory/admin/inventory.module.js"></script>
<script simpl-append-version="true" src="~/modules/inventory/admin/stock/stock-service.js"></script>
<script simpl-append-version="true" src="~/modules/inventory/admin/stock/stock-form.js"></script>
<script simpl-append-version="true" src="~/modules/inventory/admin/warehouse/warehouse-service.js"></script>
<script simpl-append-version="true" src="~/modules/inventory/admin/warehouse/warehouse-list.js"></script>
<script simpl-append-version="true" src="~/modules/inventory/admin/warehouse/warehouse-form.js"></script>

<script simpl-append-version="true" src="~/modules/shipments/admin/shipment.module.js"></script>
<script simpl-append-version="true" src="~/modules/shipments/admin/shipment/shipment-service.js"></script>
Expand Down Expand Up @@ -291,6 +294,7 @@
<li><a ui-sref="tax-classes">@Localizer["Tax Classes"]</a></li>
<li><a ui-sref="tax-rates">@Localizer["Tax Rates"]</a></li>
<li><a ui-sref="shipping-providers">@Localizer["Shipping Providers"]</a></li>
<li><a ui-sref="warehouses">@Localizer["Warehouses"]</a></li>
<li><a ui-sref="payment-providers">@Localizer["Payment Providers"]</a></li>
<li><a ui-sref="configuration">@Localizer["Settings"]</a></li>
<li><a ui-sref="localization">@Localizer["Translations"]</a></li>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System.Linq;
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SimplCommerce.Infrastructure.Data;
using SimplCommerce.Infrastructure.Web.SmartTable;
using SimplCommerce.Module.Core.Models;
using SimplCommerce.Module.Inventory.Models;
using SimplCommerce.Module.Inventory.ViewModels;

namespace SimplCommerce.Module.Inventory.Controllers
{
Expand All @@ -13,10 +17,12 @@ namespace SimplCommerce.Module.Inventory.Controllers
public class WarehouseApiController : Controller
{
private readonly IRepository<Warehouse> _warehouseRepository;
private readonly IRepository<Address> _addressRepository;

public WarehouseApiController(IRepository<Warehouse> warehouseRepository)
public WarehouseApiController(IRepository<Warehouse> warehouseRepository, IRepository<Address> addressRepository)
{
_warehouseRepository = warehouseRepository;
_addressRepository = addressRepository;
}

public async Task<IActionResult> Get()
Expand All @@ -29,5 +35,160 @@ public async Task<IActionResult> Get()

return Ok(warehouses);
}

[HttpPost("grid")]
public IActionResult List([FromBody] SmartTableParam param)
{
var query = _warehouseRepository.Query();
if (param.Search.PredicateObject != null)
{
dynamic search = param.Search.PredicateObject;

if (search.Name != null)
{
string name = $"%{search.Name}%";
query = query.Where(x => EF.Functions.Like(x.Name, name));
}
}

var warehouses = query.ToSmartTableResult(
param,
sp => new
{
sp.Id,
sp.Name
});

return Json(warehouses);
}

[HttpGet("{id}")]
public async Task<IActionResult> Get(long id)
{
var warehouse = await _warehouseRepository.Query().Include(w => w.Address).FirstOrDefaultAsync(w => w.Id == id);

if (warehouse == null)
{
return NotFound();

}

var address = warehouse.Address ?? new Address();

var model = new WarehouseVm
{
Id = warehouse.Id,
Name = warehouse.Name,
AddressId = address.Id,
ContactName = address.ContactName,
AddressLine1 = address.AddressLine1,
AddressLine2 = address.AddressLine2,
Phone = address.Phone,
StateOrProvinceId = address.StateOrProvinceId,
CountryId = address.CountryId,
City = address.City,
DistrictId = address.DistrictId,
PostalCode = address.PostalCode
};

return Json(model);
}

[HttpPost]
public async Task<IActionResult> Post([FromBody] WarehouseVm model)
{
if (ModelState.IsValid)
{
var address = new Address
{
ContactName = model.ContactName,
AddressLine1 = model.AddressLine1,
AddressLine2 = model.AddressLine2,
Phone = model.Phone,
StateOrProvinceId = model.StateOrProvinceId,
CountryId = model.CountryId,
City = model.City,
DistrictId = model.DistrictId,
PostalCode = model.PostalCode
};

var warehouse = new Warehouse
{
Name = model.Name,
Address = address
};

_warehouseRepository.Add(warehouse);

await _warehouseRepository.SaveChangesAsync();

return CreatedAtAction(nameof(Get), new { id = warehouse.Id }, null);
}
return BadRequest(ModelState);
}

[HttpPut("{id}")]
public async Task<IActionResult> Put(long id, [FromBody] WarehouseVm model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

var warehouse = await _warehouseRepository.Query().FirstOrDefaultAsync(x => x.Id == id);
if (warehouse == null)
{
return NotFound();
}

var address = await _addressRepository.Query().FirstOrDefaultAsync(a => a.Id == model.AddressId);

if (address != null)
{
address.ContactName = model.ContactName;
address.Phone = model.Phone;
address.PostalCode = model.PostalCode;
address.StateOrProvinceId = model.StateOrProvinceId;
address.CountryId = model.CountryId;
address.DistrictId = model.DistrictId;

await _addressRepository.SaveChangesAsync();
}

warehouse.Address = address;
warehouse.Name = model.Name;

await _warehouseRepository.SaveChangesAsync();

return Accepted();
}

[HttpDelete("{id}")]
public async Task<IActionResult> Delete(long id)
{
var warehouse = await _warehouseRepository.Query().Include(w => w.Address).FirstOrDefaultAsync(x => x.Id == id);
if (warehouse == null)
{
return NotFound();
}

try
{
_warehouseRepository.Remove(warehouse);
_addressRepository.Remove(warehouse.Address);

await _warehouseRepository.SaveChangesAsync();
}
catch (DbUpdateException)
{
return BadRequest(new { Error = $"The warehouse {warehouse.Name} can't not be deleted because it is referenced by other tables" });
}
catch (Exception ex)
{
return BadRequest(new { Error = ex.GetBaseException().Message });
}

return NoContent();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using SimplCommerce.Infrastructure.Models;
using SimplCommerce.Module.Core.Models;
using System.ComponentModel.DataAnnotations;

namespace SimplCommerce.Module.Inventory.Models
{
public class Warehouse : EntityBase
{
[Required]
public string Name { get; set; }

public Address Address { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimplCommerce.Module.Inventory.ViewModels
{
public class WarehouseVm
{
public long Id { get; set; }

[Required]
public string Name { get; set; }

public long AddressId { get; set; }

public string ContactName { get; set; }

public string Phone { get; set; }

public string AddressLine1 { get; set; }

public string AddressLine2 { get; set; }

public string City { get; set; }

public string PostalCode { get; set; }

public long? DistrictId { get; set; }

[Required]
public long StateOrProvinceId { get; set; }

[Required]
public long CountryId { get; set; }

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@
url: '/stocks',
templateUrl: 'modules/inventory/admin/stock/stock-form.html',
controller: 'StockFormCtrl as vm'
})
.state('warehouses', {
url: '/warehouses',
templateUrl: 'modules/inventory/admin/warehouse/warehouse-list.html',
controller: 'WarehouseListCtrl as vm'
})
.state('warehouse-create', {
url: '/warehouse/create',
templateUrl: 'modules/inventory/admin/warehouse/warehouse-form.html',
controller: 'WarehouseFormCtrl as vm'
})
.state('warehouse-edit', {
url: '/warehouses/edit/:id',
templateUrl: 'modules/inventory/admin/warehouse/warehouse-form.html',
controller: 'WarehouseFormCtrl as vm'
});
}]);
})();
Loading

0 comments on commit f318435

Please sign in to comment.