public ShipmentListResponse Sync(SyncShipmentRequest request)
        {
            ShipmentListResponse response = new ShipmentListResponse();

            try
            {
                response.Shipments = new List <ShipmentViewModel>();

                if (request.LastUpdatedAt != null)
                {
                    response.Shipments.AddRange(unitOfWork.GetShipmentRepository()
                                                .GetShipmentsNewerThan(request.CompanyId, (DateTime)request.LastUpdatedAt)
                                                ?.ConvertToShipmentViewModelList() ?? new List <ShipmentViewModel>());
                }
                else
                {
                    response.Shipments.AddRange(unitOfWork.GetShipmentRepository()
                                                .GetShipments(request.CompanyId)
                                                ?.ConvertToShipmentViewModelList() ?? new List <ShipmentViewModel>());
                }

                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Shipments = new List <ShipmentViewModel>();
                response.Success   = false;
                response.Message   = ex.Message;
            }

            return(response);
        }
Пример #2
0
        public ShipmentListResponse Sync(SyncShipmentRequest request)
        {
            ShipmentListResponse response = new ShipmentListResponse();

            try
            {
                response = WpfApiHandler.SendToApi <SyncShipmentRequest, ShipmentViewModel, ShipmentListResponse>(request, "Sync");
            }
            catch (Exception ex)
            {
                response.Shipments = new List <ShipmentViewModel>();
                response.Success   = false;
                response.Message   = ex.Message;
            }

            return(response);
        }
Пример #3
0
        public JsonResult Sync([FromBody] SyncShipmentRequest request)
        {
            ShipmentListResponse response = new ShipmentListResponse();

            try
            {
                response = this.shipmentService.Sync(request);
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }

            return(Json(response, new Newtonsoft.Json.JsonSerializerSettings()
            {
                Formatting = Newtonsoft.Json.Formatting.Indented
            }));
        }
Пример #4
0
        public void Sync(IShipmentService shipmentService, Action <int, int> callback = null)
        {
            try
            {
                SyncShipmentRequest request = new SyncShipmentRequest();
                request.CompanyId     = MainWindow.CurrentCompanyId;
                request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId);

                int toSync      = 0;
                int syncedItems = 0;

                ShipmentListResponse response = shipmentService.Sync(request);
                if (response.Success)
                {
                    toSync = response?.Shipments?.Count ?? 0;
                    var items = new List <ShipmentViewModel>(response.Shipments);

                    using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db"))
                    {
                        db.Open();
                        using (var transaction = db.BeginTransaction())
                        {
                            SqliteCommand deleteCommand = db.CreateCommand();
                            deleteCommand.CommandText = "DELETE FROM Shipments WHERE Identifier = @Identifier";

                            SqliteCommand insertCommand = db.CreateCommand();
                            insertCommand.CommandText = SqlCommandInsertPart;

                            foreach (var item in items)
                            {
                                deleteCommand.Parameters.AddWithValue("@Identifier", item.Identifier);
                                deleteCommand.ExecuteNonQuery();
                                deleteCommand.Parameters.Clear();

                                if (item.IsActive)
                                {
                                    item.IsSynced = true;

                                    insertCommand = AddCreateParameters(insertCommand, item);
                                    insertCommand.ExecuteNonQuery();
                                    insertCommand.Parameters.Clear();

                                    syncedItems++;
                                    callback?.Invoke(syncedItems, toSync);
                                }
                            }

                            transaction.Commit();
                        }
                        db.Close();
                    }
                }
                else
                {
                    throw new Exception(response.Message);
                }
            }
            catch (Exception ex)
            {
                MainWindow.ErrorMessage = ex.Message;
            }
        }