public CompanyUserListResponse Sync(SyncCompanyUserRequest request) { CompanyUserListResponse response = new CompanyUserListResponse(); List <CompanyUserViewModel> compUsers = new List <CompanyUserViewModel>(); try { if (request.LastUpdatedAt == null) { var compUsers2 = unitOfWork.GetCompanyUserRepository() ?.GetCompanyUsers() ?.ConvertToCompanyUserViewModelList(); compUsers.AddRange(compUsers2); } else { var compUsers2 = unitOfWork.GetCompanyUserRepository() ?.GetCompanyUsersNewerThan((DateTime)request.LastUpdatedAt) ?.ConvertToCompanyUserViewModelList(); compUsers.AddRange(compUsers2); } response.CompanyUsers = compUsers; response.Success = true; } catch (Exception ex) { response.Success = false; response.Message = ex.Message; response.CompanyUsers = new List <CompanyUserViewModel>(); } return(response); }
public CompanyUserListResponse Sync(SyncCompanyUserRequest request) { CompanyUserListResponse response = new CompanyUserListResponse(); try { response = WpfApiHandler.SendToApi <SyncCompanyUserRequest, List <CompanyUserViewModel>, CompanyUserListResponse>(request, "Sync"); } catch (Exception ex) { response.CompanyUsers = new List <CompanyUserViewModel>(); response.Success = false; response.Message = ex.Message; } return(response); }
public JsonResult Sync([FromBody] SyncCompanyUserRequest request) { CompanyUserListResponse response = new CompanyUserListResponse(); try { response = this.userService.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 void Sync(ICompanyUserService userService, Action <int, int> callback = null) { try { SyncCompanyUserRequest request = new SyncCompanyUserRequest(); request.LastUpdatedAt = GetLastUpdatedAt(); int toSync = 0; int syncedItems = 0; CompanyUserListResponse response = userService.Sync(request); if (response.Success) { toSync = response?.CompanyUsers?.Count ?? 0; List <CompanyUserViewModel> companyUsersFromDB = response.CompanyUsers; using (SqliteConnection db = new SqliteConnection(SQLiteHelper.SqLiteTableName)) { db.Open(); using (var transaction = db.BeginTransaction()) { SqliteCommand deleteCommand = db.CreateCommand(); deleteCommand.CommandText = "DELETE FROM CompanyUsers WHERE Identifier = @Identifier"; SqliteCommand insertCommand = db.CreateCommand(); insertCommand.CommandText = SqlCommandInsertPart; foreach (var companyUser in companyUsersFromDB) { deleteCommand.Parameters.AddWithValue("@Identifier", companyUser.Identifier); deleteCommand.ExecuteNonQuery(); deleteCommand.Parameters.Clear(); if (companyUser.IsActive) { companyUser.IsSynced = true; insertCommand = AddCreateParameters(insertCommand, companyUser); 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; } }