public ConstructionSiteNoteListResponse Sync(SyncConstructionSiteNoteRequest request) { ConstructionSiteNoteListResponse response = new ConstructionSiteNoteListResponse(); try { response.ConstructionSiteNotes = new List <ConstructionSiteNoteViewModel>(); if (request.LastUpdatedAt != null) { response.ConstructionSiteNotes.AddRange(unitOfWork.GetConstructionSiteNoteRepository() .GetConstructionSiteNotesNewerThen(request.CompanyId, (DateTime)request.LastUpdatedAt) ?.ConvertToConstructionSiteNoteViewModelList() ?? new List <ConstructionSiteNoteViewModel>()); } else { response.ConstructionSiteNotes.AddRange(unitOfWork.GetConstructionSiteNoteRepository() .GetConstructionSiteNotes(request.CompanyId) ?.ConvertToConstructionSiteNoteViewModelList() ?? new List <ConstructionSiteNoteViewModel>()); } response.Success = true; } catch (Exception ex) { response.ConstructionSiteNotes = new List <ConstructionSiteNoteViewModel>(); response.Success = false; response.Message = ex.Message; } return(response); }
public ConstructionSiteNoteListResponse Sync(SyncConstructionSiteNoteRequest request) { ConstructionSiteNoteListResponse response = new ConstructionSiteNoteListResponse(); try { response = WpfApiHandler.SendToApi <SyncConstructionSiteNoteRequest, ConstructionSiteNoteViewModel, ConstructionSiteNoteListResponse>(request, "Sync"); } catch (Exception ex) { response.ConstructionSiteNotes = new List <ConstructionSiteNoteViewModel>(); response.Success = false; response.Message = ex.Message; } return(response); }
public JsonResult Sync([FromBody] SyncConstructionSiteNoteRequest request) { ConstructionSiteNoteListResponse response = new ConstructionSiteNoteListResponse(); try { response = this.ConstructionSiteNoteService.Sync(request); } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(Json(response, new Newtonsoft.Json.JsonSerializerSettings() { Formatting = Newtonsoft.Json.Formatting.Indented })); }
//public ConstructionSiteNoteListResponse GetUnSyncedNotes(int companyId) //{ // ConstructionSiteNoteListResponse response = new ConstructionSiteNoteListResponse(); // List<ConstructionSiteNoteViewModel> viewModels = new List<ConstructionSiteNoteViewModel>(); // using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db")) // { // db.Open(); // try // { // SqliteCommand selectCommand = new SqliteCommand( // SqlCommandSelectPart + // "FROM ConstructionSiteNotes " + // "WHERE CompanyId = @CompanyId AND IsSynced = 0 " + // "ORDER BY Id DESC;", db); // selectCommand.Parameters.AddWithValue("@CompanyId", companyId); // SqliteDataReader query = selectCommand.ExecuteReader(); // while (query.Read()) // { // int counter = 0; // ConstructionSiteNoteViewModel dbEntry = new ConstructionSiteNoteViewModel(); // dbEntry.Id = SQLiteHelper.GetInt(query, ref counter); // dbEntry.Identifier = SQLiteHelper.GetGuid(query, ref counter); // dbEntry.ConstructionSite = SQLiteHelper.GetConstructionSite(query, ref counter); // dbEntry.Note = SQLiteHelper.GetString(query, ref counter); // dbEntry.NoteDate = SQLiteHelper.GetDateTime(query, ref counter); // dbEntry.IsSynced = SQLiteHelper.GetBoolean(query, ref counter); // dbEntry.UpdatedAt = SQLiteHelper.GetDateTime(query, ref counter); // dbEntry.CreatedBy = SQLiteHelper.GetCreatedBy(query, ref counter); // dbEntry.Company = SQLiteHelper.GetCompany(query, ref counter); // viewModels.Add(dbEntry); // } // } // catch (SqliteException error) // { // MainWindow.ErrorMessage = error.Message; // response.Success = false; // response.Message = error.Message; // response.ConstructionSiteNotes = new List<ConstructionSiteNoteViewModel>(); // return response; // } // db.Close(); // } // response.Success = true; // response.ConstructionSiteNotes = viewModels; // return response; //} #endregion #region Sync public void Sync(IConstructionSiteNoteService ConstructionSiteNoteService, Action <int, int> callback = null) { try { SyncConstructionSiteNoteRequest request = new SyncConstructionSiteNoteRequest(); request.CompanyId = MainWindow.CurrentCompanyId; request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId); int toSync = 0; int syncedItems = 0; ConstructionSiteNoteListResponse response = ConstructionSiteNoteService.Sync(request); if (response.Success) { toSync = response?.ConstructionSiteNotes?.Count ?? 0; List <ConstructionSiteNoteViewModel> items = response.ConstructionSiteNotes; using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db")) { db.Open(); using (var transaction = db.BeginTransaction()) { SqliteCommand deleteCommand = db.CreateCommand(); deleteCommand.CommandText = "DELETE FROM ConstructionSiteNotes 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; } }