public async Task <CommandResult> ExecuteAsync(int branchId, List <EnableProductViewModel> listProduct) { try { listProduct = listProduct ?? new List <EnableProductViewModel>(); var data = await _branchProductRepository.Table .Where(n => n.BranchId == branchId) .ToListAsync(); int length = listProduct.Count <= data.Count ? listProduct.Count : data.Count; for (int i = 0; i < length; i++) { data[i].ProductId = listProduct[i].Id; } await _branchProductRepository.UpdateAsync(data); if (listProduct.Count > data.Count) { List <CsmsBranchProduct> insertList = new List <CsmsBranchProduct>(); for (int i = data.Count; i < listProduct.Count; i++) { insertList.Add(new CsmsBranchProduct(branchId, listProduct[i].Id)); } await _branchProductRepository.InsertAsync(insertList); } else { if (listProduct.Count < data.Count) { List <CsmsBranchProduct> deleteList = new List <CsmsBranchProduct>(); deleteList.AddRange(data.GetRange(listProduct.Count, data.Count - listProduct.Count)); await _branchProductRepository.DeleteAsync(deleteList); } } var result = await _getProductByBranchIdQuery.ExecuteAsync(branchId); return(CommandResult.SuccessWithData(result)); } catch (Exception) { return(CommandResult.Failed(new CommandResultError() { Code = (int)HttpStatusCode.InternalServerError, Description = MessageError.InternalServerError })); } }
public async Task <IActionResult> GetProductByBranchIdAsync(int branchId) { var result = await _getProductByBranchIdQuery.ExecuteAsync(branchId); return(new ObjectResult(result)); }