Esempio n. 1
0
        public ServiceDeliveryListResponse Sync(SyncServiceDeliveryRequest request)
        {
            ServiceDeliveryListResponse response = new ServiceDeliveryListResponse();

            try
            {
                response.ServiceDeliverys = new List <ServiceDeliveryViewModel>();

                if (request.LastUpdatedAt != null)
                {
                    response.ServiceDeliverys.AddRange(unitOfWork.GetServiceDeliveryRepository()
                                                       .GetServiceDeliverysNewerThen(request.CompanyId, (DateTime)request.LastUpdatedAt)
                                                       ?.ConvertToServiceDeliveryViewModelList() ?? new List <ServiceDeliveryViewModel>());
                }
                else
                {
                    response.ServiceDeliverys.AddRange(unitOfWork.GetServiceDeliveryRepository()
                                                       .GetServiceDeliverys(request.CompanyId)
                                                       ?.ConvertToServiceDeliveryViewModelList() ?? new List <ServiceDeliveryViewModel>());
                }

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

            return(response);
        }
Esempio n. 2
0
        public ServiceDeliveryListResponse Sync(SyncServiceDeliveryRequest request)
        {
            ServiceDeliveryListResponse response = new ServiceDeliveryListResponse();

            try
            {
                response = WpfApiHandler.SendToApi <SyncServiceDeliveryRequest, ServiceDeliveryViewModel, ServiceDeliveryListResponse>(request, "Sync");
            }
            catch (Exception ex)
            {
                response.ServiceDeliverys = new List <ServiceDeliveryViewModel>();
                response.Success          = false;
                response.Message          = ex.Message;
            }

            return(response);
        }
        public ServiceDeliveryListResponse GetServiceDeliverysForPopup(int companyId, string filterString)
        {
            ServiceDeliveryListResponse     response         = new ServiceDeliveryListResponse();
            List <ServiceDeliveryViewModel> serviceDeliverys = new List <ServiceDeliveryViewModel>();

            using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db"))
            {
                db.Open();
                try
                {
                    SqliteCommand selectCommand = new SqliteCommand(
                        SqlCommandSelectPart +
                        "FROM ServiceDeliverys " +
                        "WHERE (@Name IS NULL OR @Name = '' OR Name LIKE @Name OR Code LIKE @Name) " +
                        "AND CompanyId = @CompanyId " +
                        "ORDER BY IsSynced, Id DESC " +
                        "LIMIT @ItemsPerPage;", db);

                    selectCommand.Parameters.AddWithValue("@Name", ((object)filterString) != null ? "%" + filterString + "%" : "");
                    selectCommand.Parameters.AddWithValue("@CompanyId", ((object)filterString) != null ? companyId : 0);
                    selectCommand.Parameters.AddWithValue("@ItemsPerPage", 100);

                    SqliteDataReader query = selectCommand.ExecuteReader();

                    while (query.Read())
                    {
                        ServiceDeliveryViewModel dbEntry = Read(query);
                        serviceDeliverys.Add(dbEntry);
                    }
                }
                catch (SqliteException error)
                {
                    MainWindow.ErrorMessage   = error.Message;
                    response.Success          = false;
                    response.Message          = error.Message;
                    response.ServiceDeliverys = new List <ServiceDeliveryViewModel>();
                    return(response);
                }
                db.Close();
            }
            response.Success          = true;
            response.ServiceDeliverys = serviceDeliverys;
            return(response);
        }
Esempio n. 4
0
        public ServiceDeliveryListResponse GetServiceDeliverys(int companyId)
        {
            ServiceDeliveryListResponse response = new ServiceDeliveryListResponse();

            try
            {
                response.ServiceDeliverys = unitOfWork.GetServiceDeliveryRepository().GetServiceDeliverys(companyId)
                                            .ConvertToServiceDeliveryViewModelList();
                response.Success = true;
            }
            catch (Exception ex)
            {
                response.ServiceDeliverys = new List <ServiceDeliveryViewModel>();
                response.Success          = false;
                response.Message          = ex.Message;
            }

            return(response);
        }
Esempio n. 5
0
        public ServiceDeliveryListResponse GetServiceDeliverys(int companyId)
        {
            ServiceDeliveryListResponse response = new ServiceDeliveryListResponse();

            try
            {
                response = WpfApiHandler.GetFromApi <List <ServiceDeliveryViewModel>, ServiceDeliveryListResponse>("GetServiceDeliverys", new Dictionary <string, string>()
                {
                    { "CompanyId", companyId.ToString() }
                });
            }
            catch (Exception ex)
            {
                response.ServiceDeliverys = new List <ServiceDeliveryViewModel>();
                response.Success          = false;
                response.Message          = ex.Message;
            }

            return(response);
        }
        public JsonResult Sync([FromBody] SyncServiceDeliveryRequest request)
        {
            ServiceDeliveryListResponse response = new ServiceDeliveryListResponse();

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

            JsonResult result = Json(response, new Newtonsoft.Json.JsonSerializerSettings()
            {
                Formatting            = Newtonsoft.Json.Formatting.Indented,
                ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize
            });

            return(result);
        }
        public JsonResult GetServiceDeliverys(int companyId)
        {
            ServiceDeliveryListResponse response = new ServiceDeliveryListResponse();

            try
            {
                response = serviceDeliveryService.GetServiceDeliverys(companyId);
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
                Console.WriteLine(ex.Message);
            }

            JsonResult result = Json(response, new Newtonsoft.Json.JsonSerializerSettings()
            {
                Formatting            = Newtonsoft.Json.Formatting.Indented,
                ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize
            });

            return(result);
        }
        public ServiceDeliveryListResponse GetServiceDeliverysByPage(int companyId, ServiceDeliveryViewModel serviceDeliverySearchObject, int currentPage = 1, int itemsPerPage = 50)
        {
            ServiceDeliveryListResponse     response         = new ServiceDeliveryListResponse();
            List <ServiceDeliveryViewModel> serviceDeliverys = new List <ServiceDeliveryViewModel>();

            using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db"))
            {
                db.Open();
                try
                {
                    SqliteCommand selectCommand = new SqliteCommand(
                        SqlCommandSelectPart +
                        "FROM ServiceDeliverys " +
                        "WHERE (@Name IS NULL OR @Name = '' OR Name LIKE @Name) " +
                        "AND CompanyId = @CompanyId " +
                        "ORDER BY IsSynced, ServerId " +
                        "LIMIT @ItemsPerPage OFFSET @Offset;", db);

                    selectCommand.Parameters.AddWithValue("@Name", ((object)serviceDeliverySearchObject.Search_Name) != null ? "%" + serviceDeliverySearchObject.Search_Name + "%" : "");
                    selectCommand.Parameters.AddWithValue("@CompanyId", companyId);
                    selectCommand.Parameters.AddWithValue("@ItemsPerPage", itemsPerPage);
                    selectCommand.Parameters.AddWithValue("@Offset", (currentPage - 1) * itemsPerPage);

                    SqliteDataReader query = selectCommand.ExecuteReader();

                    while (query.Read())
                    {
                        ServiceDeliveryViewModel dbEntry = Read(query);
                        serviceDeliverys.Add(dbEntry);
                    }

                    selectCommand = new SqliteCommand(
                        "SELECT Count(*) " +
                        "FROM ServiceDeliverys " +
                        "WHERE (@Name IS NULL OR @Name = '' OR Name LIKE @Name) " +
                        "AND CompanyId = @CompanyId;", db);

                    selectCommand.Parameters.AddWithValue("@Name", ((object)serviceDeliverySearchObject.Search_Name) != null ? "%" + serviceDeliverySearchObject.Search_Name + "%" : "");
                    selectCommand.Parameters.AddWithValue("@CompanyId", companyId);

                    query = selectCommand.ExecuteReader();

                    if (query.Read())
                    {
                        response.TotalItems = query.GetInt32(0);
                    }
                }
                catch (SqliteException error)
                {
                    MainWindow.ErrorMessage   = error.Message;
                    response.Success          = false;
                    response.Message          = error.Message;
                    response.ServiceDeliverys = new List <ServiceDeliveryViewModel>();
                    return(response);
                }
                db.Close();
            }
            response.Success          = true;
            response.ServiceDeliverys = serviceDeliverys;
            return(response);
        }
        public void Sync(IServiceDeliveryService serviceDeliveryService, Action <int, int> callback = null)
        {
            try
            {
                SyncServiceDeliveryRequest request = new SyncServiceDeliveryRequest();
                request.CompanyId     = MainWindow.CurrentCompanyId;
                request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId);

                int toSync      = 0;
                int syncedItems = 0;

                ServiceDeliveryListResponse response = serviceDeliveryService.Sync(request);
                if (response.Success)
                {
                    toSync = response?.ServiceDeliverys?.Count ?? 0;
                    List <ServiceDeliveryViewModel> serviceDeliverysFromDB = response.ServiceDeliverys;

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

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

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

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

                                    insertCommand = AddCreateParameters(insertCommand, serviceDelivery);
                                    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;
            }
        }