public PhysicalPersonNoteListResponse Sync(SyncPhysicalPersonNoteRequest request) { PhysicalPersonNoteListResponse response = new PhysicalPersonNoteListResponse(); try { response.PhysicalPersonNotes = new List <PhysicalPersonNoteViewModel>(); if (request.LastUpdatedAt != null) { response.PhysicalPersonNotes.AddRange(unitOfWork.GetPhysicalPersonNoteRepository() .GetPhysicalPersonNotesNewerThen(request.CompanyId, (DateTime)request.LastUpdatedAt) ?.ConvertToPhysicalPersonNoteViewModelList() ?? new List <PhysicalPersonNoteViewModel>()); } else { response.PhysicalPersonNotes.AddRange(unitOfWork.GetPhysicalPersonNoteRepository() .GetPhysicalPersonNotes(request.CompanyId) ?.ConvertToPhysicalPersonNoteViewModelList() ?? new List <PhysicalPersonNoteViewModel>()); } response.Success = true; } catch (Exception ex) { response.PhysicalPersonNotes = new List <PhysicalPersonNoteViewModel>(); response.Success = false; response.Message = ex.Message; } return(response); }
public PhysicalPersonNoteListResponse Sync(SyncPhysicalPersonNoteRequest request) { PhysicalPersonNoteListResponse response = new PhysicalPersonNoteListResponse(); try { response = WpfApiHandler.SendToApi <SyncPhysicalPersonNoteRequest, PhysicalPersonNoteViewModel, PhysicalPersonNoteListResponse>(request, "Sync"); } catch (Exception ex) { response.PhysicalPersonNotes = new List <PhysicalPersonNoteViewModel>(); response.Success = false; response.Message = ex.Message; } return(response); }
public JsonResult Sync([FromBody] SyncPhysicalPersonNoteRequest request) { PhysicalPersonNoteListResponse response = new PhysicalPersonNoteListResponse(); try { response = this.PhysicalPersonNoteService.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 PhysicalPersonNoteListResponse GetPhysicalPersonNotesByPhysicalPerson(int companyId, Guid PhysicalPersonIdentifier) { PhysicalPersonNoteListResponse response = new PhysicalPersonNoteListResponse(); List <PhysicalPersonNoteViewModel> PhysicalPersonNotes = new List <PhysicalPersonNoteViewModel>(); using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db")) { db.Open(); try { SqliteCommand selectCommand = new SqliteCommand( SqlCommandSelectPart + "FROM PhysicalPersonNotes " + "WHERE PhysicalPersonIdentifier = @PhysicalPersonIdentifier " + "AND CompanyId = @CompanyId " + "ORDER BY IsSynced, Id DESC;", db); selectCommand.Parameters.AddWithValue("@PhysicalPersonIdentifier", PhysicalPersonIdentifier); selectCommand.Parameters.AddWithValue("@CompanyId", companyId); SqliteDataReader query = selectCommand.ExecuteReader(); while (query.Read()) { PhysicalPersonNoteViewModel dbEntry = Read(query); PhysicalPersonNotes.Add(dbEntry); } } catch (SqliteException error) { MainWindow.ErrorMessage = error.Message; response.Success = false; response.Message = error.Message; response.PhysicalPersonNotes = new List <PhysicalPersonNoteViewModel>(); return(response); } db.Close(); } response.Success = true; response.PhysicalPersonNotes = PhysicalPersonNotes; return(response); }
//public PhysicalPersonNoteListResponse GetUnSyncedNotes(int companyId) //{ // PhysicalPersonNoteListResponse response = new PhysicalPersonNoteListResponse(); // List<PhysicalPersonNoteViewModel> viewModels = new List<PhysicalPersonNoteViewModel>(); // using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db")) // { // db.Open(); // try // { // SqliteCommand selectCommand = new SqliteCommand( // SqlCommandSelectPart + // "FROM PhysicalPersonNotes " + // "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; // PhysicalPersonNoteViewModel dbEntry = new PhysicalPersonNoteViewModel(); // dbEntry.Id = SQLiteHelper.GetInt(query, ref counter); // dbEntry.Identifier = SQLiteHelper.GetGuid(query, ref counter); // dbEntry.PhysicalPerson = SQLiteHelper.GetPhysicalPerson(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.PhysicalPersonNotes = new List<PhysicalPersonNoteViewModel>(); // return response; // } // db.Close(); // } // response.Success = true; // response.PhysicalPersonNotes = viewModels; // return response; //} #endregion #region Sync public void Sync(IPhysicalPersonNoteService physicalPersonNoteService, Action <int, int> callback = null) { try { SyncPhysicalPersonNoteRequest request = new SyncPhysicalPersonNoteRequest(); request.CompanyId = MainWindow.CurrentCompanyId; request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId); int toSync = 0; int syncedItems = 0; PhysicalPersonNoteListResponse response = physicalPersonNoteService.Sync(request); if (response.Success) { toSync = response?.PhysicalPersonNotes?.Count ?? 0; List <PhysicalPersonNoteViewModel> items = response.PhysicalPersonNotes; using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db")) { db.Open(); using (var transaction = db.BeginTransaction()) { SqliteCommand deleteCommand = db.CreateCommand(); deleteCommand.CommandText = "DELETE FROM PhysicalPersonNotes 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; } }