public async Task <IActionResult> Delete([FromRoute] int id)
        {
            APIResult apiResult;
            var       record = await OrderItemService.GetAsync(id);

            var result = mapper.Map <OrderItemDto>(record);

            if (record != null)
            {
                var isSuccessful = await OrderItemService.DeleteAsync(record);

                if (isSuccessful)
                {
                    apiResult = APIResultFactory.Build(true, StatusCodes.Status202Accepted,
                                                       ErrorMessageEnum.None, payload: result);
                }
                else
                {
                    apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK,
                                                       ErrorMessageEnum.無法刪除紀錄, payload: result);
                }
            }
            else
            {
                apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK,
                                                   ErrorMessageEnum.沒有任何符合資料存在, payload: result);
            }
            return(Ok(apiResult));
        }
Exemple #2
0
        public async Task <IActionResult> Delete([FromRoute] int id)
        {
            APIResult apiResult;
            var       record = await OrderItemService.GetAsync(id);

            var result = mapper.Map <OrderItemDto>(record);

            if (record != null && record.Id != 0)
            {
                #region 刪除記錄前的紀錄完整性檢查
                VerifyRecordResult verify = await OrderItemService.BeforeDeleteCheckAsync(record);

                if (verify.Success == false)
                {
                    apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK,
                                                       ErrorMessageMappingHelper.Instance.GetErrorMessage(verify.MessageId),
                                                       payload: result);
                    return(Ok(apiResult));
                }
                #endregion

                var verifyRecordResult = await OrderItemService.DeleteAsync(id);

                if (verifyRecordResult.Success)
                {
                    apiResult = APIResultFactory.Build(true, StatusCodes.Status202Accepted,
                                                       ErrorMessageEnum.None, payload: null);
                }
                else
                {
                    if (verifyRecordResult.MessageId == ErrorMessageEnum.客製化文字錯誤訊息)
                    {
                        if (verifyRecordResult.Exception == null)
                        {
                            apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK,
                                                               verifyRecordResult.Message, payload: result);
                        }
                        else
                        {
                            apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK,
                                                               verifyRecordResult.Message, payload: result,
                                                               exceptionMessage: verifyRecordResult.Exception.Message,
                                                               replaceExceptionMessage: true);
                        }
                    }
                    else
                    {
                        apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK,
                                                           verifyRecordResult.MessageId, payload: result);
                    }
                }
            }
            else
            {
                apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK,
                                                   ErrorMessageEnum.沒有任何符合資料存在, payload: result);
            }
            return(Ok(apiResult));
        }
Exemple #3
0
        public async Task OnCommandClicked(CommandClickEventArgs <OrderItemAdapterModel> args)
        {
            OrderItemAdapterModel item = args.RowData as OrderItemAdapterModel;

            if (args.CommandColumn.ButtonOption.IconCss == ButtonIdHelper.ButtonIdEdit)
            {
                #region 點選 修改紀錄 按鈕
                CurrentRecord         = item.Clone();
                EditRecordDialogTitle = "修改紀錄";
                IsShowEditRecord      = true;
                isNewRecordMode       = false;
                #endregion
            }
            else if (args.CommandColumn.ButtonOption.IconCss == ButtonIdHelper.ButtonIdDelete)
            {
                #region 點選 刪除紀錄 按鈕
                CurrentNeedDeleteRecord = item;

                #region 檢查關聯資料是否存在
                var checkedResult = await CurrentService
                                    .BeforeDeleteCheckAsync(CurrentNeedDeleteRecord);

                await Task.Delay(100);

                if (checkedResult.Success == false)
                {
                    MessageBox.Show("400px", "200px", "警告",
                                    ErrorMessageMappingHelper.Instance.GetErrorMessage(checkedResult.MessageId),
                                    MessageBox.HiddenAsync);
                    await Task.Yield();

                    await thisView.NeedRefreshAsync();

                    return;
                }
                #endregion

                #region 刪除這筆紀錄
                await Task.Yield();

                var checkTask = ConfirmMessageBox.ShowAsync("400px", "200px", "警告",
                                                            "確認要刪除這筆紀錄嗎?", ConfirmMessageBox.HiddenAsync);
                await thisView.NeedRefreshAsync();

                var checkAgain = await checkTask;
                if (checkAgain == true)
                {
                    var verifyRecordResult = await CurrentService.DeleteAsync(CurrentNeedDeleteRecord.Id);

                    await TranscationResultHelper.CheckDatabaseResult(MessageBox, verifyRecordResult);

                    dataGrid.RefreshGrid();
                }
                #endregion
                #endregion
            }
        }
        private async void SaveOrderCommandExecute(object obj)
        {
            Logger.For(this).Info("Saving order");
            IsBusy = true;
            var existingOrderItems = Order.OrderItems;
            var newOrderItems      = OrderItems.Where(checkableItem => checkableItem.IsChecked).Select(checkableItem => checkableItem.Item);

            var addedOrderItems   = newOrderItems.Where(orderItem => !existingOrderItems.Any(existingOrderItem => existingOrderItem.Id == orderItem.Id)).ToList();
            var deletedOrderItems = existingOrderItems.Where(existingOrderItem => !newOrderItems.Any(orderItem => orderItem.Id == existingOrderItem.Id)).ToList();
            var updatedOrderItems = existingOrderItems.Where(existingOrderItem => !deletedOrderItems.Any(deletedOrderItem => deletedOrderItem.Id == existingOrderItem.Id)).ToList();

            try
            {
                Logger.For(this).Info("Adding new order items");
                foreach (OrderItemDto orderItem in addedOrderItems)
                {
                    orderItem.OrderId = Order.Id;
                    var createdOrderItem = await orderItemService.CreateAsync(orderItem);

                    Order.OrderItems.Add(createdOrderItem);
                }

                Logger.For(this).Info("Removing older order items");
                foreach (OrderItemDto orderItem in deletedOrderItems)
                {
                    await orderItemService.DeleteAsync(orderItem.Id);

                    Order.OrderItems.Remove(orderItem);
                }

                Logger.For(this).Info("Updating existing order items");
                foreach (OrderItemDto orderItem in updatedOrderItems)
                {
                    await orderItemService.UpdateAsync(orderItem);

                    var tempOrderItem = Order.OrderItems.Find(o => o.Id == orderItem.Id);
                    Order.OrderItems.Remove(tempOrderItem);
                    Order.OrderItems.Add(orderItem);
                }

                IsBusy = false;
                Logger.For(this).Info("Order is saved");
                Messenger.Default.Send <WindowMessege, bool?>(WindowMessege.CloseEditOrderWindow, true);
                Logger.For(this).Info("Close EditOrderWindow");
            }
            catch (NoInternetConnectionException ex)
            {
                Logger.For(this).Error(ex.Message, ex);
                messageService.ShowMessage(ex.Message, "Error", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
            }
            catch (HttpRequestException ex)
            {
                Logger.For(this).Error(ex.Message, ex);
                messageService.ShowMessage(ex.Message, "Error", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
            }
            IsBusy = false;
        }
        public async Task RemoveThisRecord(bool NeedDelete)
        {
            if (NeedDelete == true)
            {
                await CurrentService.DeleteAsync(mapper.Map <OrderItem>(CurrentNeedDeleteRecord));

                dataGrid.RefreshGrid();
            }
            ConfirmMessageBox.Hidden();
        }
        public async Task RemoveThisRecord(bool NeedDelete)
        {
            if (NeedDelete == true)
            {
                var verifyRecordResult = await CurrentService.DeleteAsync(CurrentNeedDeleteRecord.Id);

                await TranscationResultHelper.CheckDatabaseResult(MessageBox, verifyRecordResult);

                dataGrid.RefreshGrid();
            }
            ConfirmMessageBox.Hidden();
        }
        public async Task <ObjectResult> Delete(int id)
        {
            var response = await service.DeleteAsync(id);

            return(response);
        }