public async Task <bool> UploadInBackground() { try { Cursor.Current = Cursors.AppStarting; this.PopulateTags(); await this.PrepareModel(); var res = await ChummerHub.Client.Backend.Utils.PostSINnerAsync(this); var response = await Backend.Utils.HandleError(res) as ResultBase; if (response.CallSuccess == true) { var jsonResultString = res.Response.Content.ReadAsStringAsync().Result; try { ResultSinnerPostSIN objIds = Newtonsoft.Json.JsonConvert.DeserializeObject <ResultSinnerPostSIN>(jsonResultString); throw new NotImplementedException("Just keep coding!"); System.Collections.ICollection ids = objIds as System.Collections.ICollection; if (ids == null || ids.Count == 0) { string msg = "ChummerHub did not return a valid Id for sinner " + this.MyCharacter.Name + "."; System.Diagnostics.Trace.TraceError(msg); throw new ArgumentException(msg); } var cur = ids.GetEnumerator(); cur.MoveNext(); if (!Guid.TryParse(cur.Current.ToString(), out var sinGuid)) { string msg = "ChummerHub did not return a valid IdArray for sinner " + this.MyCharacter.Alias + "."; System.Diagnostics.Trace.TraceError(msg); throw new ArgumentException(msg); } this.MySINnerFile.Id = sinGuid; } catch (Exception ex) { System.Diagnostics.Trace.TraceError(ex.ToString()); throw; } } //System.Diagnostics.Trace.TraceInformation("Character " + this.MyCharacter.Alias + " posted with ID " + this.MySINnerFile.Id); //ChummerHub.Client.Backend.Utils.UploadChummerFileAsync(this).ContinueWith((uploadtask) => //{ // if (uploadtask.Status != TaskStatus.RanToCompletion) // { // if (uploadtask.Exception != null) // throw uploadtask.Exception; // return; // } // if (uploadtask.Result?.Response?.StatusCode != HttpStatusCode.OK) // { // System.Diagnostics.Trace.TraceWarning("Upload failed for Character " + this.MyCharacter.Alias + ": " + uploadtask.Result?.Response?.StatusCode); // } // else // System.Diagnostics.Trace.TraceInformation("Character " + this.MyCharacter.Alias + " uploaded."); //}); } catch (Exception e) { System.Diagnostics.Trace.TraceError(e.Message, e); Console.WriteLine(e.ToString()); MessageBox.Show(e.ToString()); } finally { Cursor.Current = Cursors.Default; } throw new NotImplementedException("Just keep coding!"); }
private async Task <ActionResult <ResultSinnerPostSIN> > PostSINnerInternal(UploadInfoObject uploadInfo) { ResultSinnerPostSIN res; _logger.LogTrace("Post SINnerInternalt: " + uploadInfo + "."); ApplicationUser user = null; SINner sinner = null; try { if (!ModelState.IsValid) { var errors = ModelState.Select(x => x.Value.Errors) .Where(y => y.Count > 0) .ToList(); string msg = "ModelState is invalid: "; foreach (var err in errors) { foreach (var singleerr in err) { msg += Environment.NewLine + "\t" + singleerr.ToString(); } } var e = new HubException(msg); res = new ResultSinnerPostSIN(e); return(BadRequest(res)); } if (uploadInfo.UploadDateTime == null) { uploadInfo.UploadDateTime = DateTime.Now; } if (uploadInfo.Client != null) { if (!UploadClientExists(uploadInfo.Client.Id)) { _context.UploadClients.Add(uploadInfo.Client); } else { _context.UploadClients.Attach(uploadInfo.Client); _context.Entry(uploadInfo.Client).State = EntityState.Modified; _context.Entry(uploadInfo.Client).CurrentValues.SetValues(uploadInfo.Client); } } var returncode = HttpStatusCode.OK; user = await _signInManager.UserManager.FindByNameAsync(User.Identity.Name); foreach (var tempsinner in uploadInfo.SINners) { sinner = tempsinner; if (sinner.Id.ToString() == "string") { sinner.Id = Guid.Empty; } if (String.IsNullOrEmpty(sinner.MyExtendedAttributes.JsonSummary)) { var e = new ArgumentException("sinner " + sinner.Id + ": JsonSummary == null"); res = new ResultSinnerPostSIN(e); return(BadRequest(res)); } if (sinner.LastChange == null) { var e = new ArgumentException("Sinner " + sinner.Id + ": LastChange not set!"); res = new ResultSinnerPostSIN(e); return(BadRequest(res)); } if ((sinner.SINnerMetaData.Visibility.Id == null) || (sinner.SINnerMetaData.Visibility.Id == Guid.Empty)) { sinner.SINnerMetaData.Visibility.Id = Guid.NewGuid(); } if ((sinner.MyExtendedAttributes.Id == null) || (sinner.MyExtendedAttributes.Id == Guid.Empty)) { sinner.MyExtendedAttributes.Id = Guid.NewGuid(); } var oldsinner = (from a in _context.SINners .Include(a => a.MyExtendedAttributes) .Include(a => a.SINnerMetaData) .Include(a => a.SINnerMetaData.Visibility) .Include(a => a.SINnerMetaData.Visibility.UserRights) .Include(b => b.MyGroup) where a.Id == sinner.Id select a).FirstOrDefault(); if (oldsinner != null) { var canedit = await CheckIfUpdateSINnerFile(oldsinner.Id.Value, user); if (canedit == null) { string msg = "SINner " + sinner.Id + " is not editable for user " + user.Email + "."; var e = new NoUserRightException(msg); res = new ResultSinnerPostSIN(e); return(BadRequest(res)); } var olduserrights = oldsinner.SINnerMetaData.Visibility.UserRights.ToList(); oldsinner.SINnerMetaData.Visibility.UserRights.Clear(); _context.UserRights.RemoveRange(olduserrights); //check if ANY visibility-data was uploaded if (sinner.SINnerMetaData.Visibility.UserRights.Any()) { bool userfound = false; foreach (var ur in sinner.SINnerMetaData.Visibility.UserRights) { if (ur.EMail.ToLowerInvariant() == user.Email.ToLowerInvariant()) { ur.CanEdit = true; userfound = true; } ur.Id = Guid.NewGuid(); ur.SINnerId = sinner.Id; _context.UserRights.Add(ur); } if (!userfound) { SINnerUserRight ownUser = new SINnerUserRight(); ownUser.Id = Guid.NewGuid(); ownUser.SINnerId = sinner.Id; ownUser.CanEdit = true; ownUser.EMail = user.Email; sinner.SINnerMetaData.Visibility.UserRights.Add(ownUser); _context.UserRights.Add(ownUser); } } else { //no userrights where uploaded. sinner.SINnerMetaData.Visibility.UserRights = olduserrights; } } else { var ownuserfound = false; var list = sinner.SINnerMetaData.Visibility.UserRights.ToList(); foreach (var ur in list) { ur.SINnerId = sinner.Id; if (ur.EMail.ToLowerInvariant() == "*****@*****.**".ToLowerInvariant()) { sinner.SINnerMetaData.Visibility.UserRights.Remove(ur); } if (ur.EMail.ToLowerInvariant() == user.Email.ToLowerInvariant()) { ownuserfound = true; } } if (!ownuserfound) { SINnerUserRight ownright = new SINnerUserRight(); ownright.CanEdit = true; ownright.EMail = user.Email; ownright.SINnerId = sinner.Id; ownright.Id = Guid.NewGuid(); sinner.SINnerMetaData.Visibility.UserRights.Add(ownright); } } foreach (var tag in sinner.SINnerMetaData.Tags) { tag.SetSinnerIdRecursive(sinner.Id); } sinner.UploadClientId = uploadInfo.Client.Id; SINner dbsinner = await CheckIfUpdateSINnerFile(sinner.Id.Value, user, true); SINnerGroup oldgroup = null; if (dbsinner != null) { oldgroup = dbsinner.MyGroup; //_context.SINners.Attach(dbsinner); if (String.IsNullOrEmpty(sinner.GoogleDriveFileId)) { sinner.GoogleDriveFileId = dbsinner.GoogleDriveFileId; } if (String.IsNullOrEmpty(sinner.DownloadUrl)) { sinner.DownloadUrl = dbsinner.DownloadUrl; } var alltags = await _context.Tags.Where(a => a.SINnerId == dbsinner.Id).Select(a => a.Id).ToListAsync(); foreach (var id in alltags) { var tag = from a in _context.Tags where a.Id == id select a; if (tag.Any()) { _context.Tags.Remove(tag.FirstOrDefault()); } } _context.UserRights.RemoveRange(dbsinner.SINnerMetaData.Visibility.UserRights); _context.SINnerVisibility.Remove(dbsinner.SINnerMetaData.Visibility); _context.SINnerExtendedMetaData.Remove(dbsinner.MyExtendedAttributes); _context.SINnerMetaData.Remove(dbsinner.SINnerMetaData); _context.SINners.Remove(dbsinner); dbsinner.SINnerMetaData.Visibility.UserRights.Clear(); dbsinner.SINnerMetaData.Visibility.UserRights = null; dbsinner.SINnerMetaData.Visibility = null; dbsinner.SINnerMetaData.Tags = null; dbsinner.SINnerMetaData = null; dbsinner.MyExtendedAttributes = null; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException ex) { foreach (var entry in ex.Entries) { if (entry.Entity is SINner || entry.Entity is Tag || entry.Entity is SINnerExtended || entry.Entity is SINnerGroup || entry.Entity is SINnerUserRight || entry.Entity is SINnerMetaData) { try { Utils.DbUpdateConcurrencyExceptionHandler(entry, _logger); } catch (Exception e) { res = new ResultSinnerPostSIN(e); return(BadRequest(res)); } } else { var e = new NotSupportedException( "(Codepoint 1) Don't know how to handle concurrency conflicts for " + entry.Metadata.Name); res = new ResultSinnerPostSIN(e); return(BadRequest(res)); } } } catch (DbUpdateException ex) { res = new ResultSinnerPostSIN(ex); return(BadRequest(res)); } await _context.SINners.AddAsync(sinner); string msg = "Sinner " + sinner.Id + " updated: " + _context.Entry(dbsinner).State.ToString(); msg += Environment.NewLine + Environment.NewLine + "LastChange: " + dbsinner.LastChange; _logger.LogError(msg); List <Tag> taglist = sinner.SINnerMetaData.Tags; UpdateEntityEntries(taglist); } else { returncode = HttpStatusCode.Created; sinner.MyGroup = null; _context.SINners.Add(sinner); } try { await _context.SaveChangesAsync(); if (oldgroup != null) { var roles = await _userManager.GetRolesAsync(user); await SINnerGroupController.PutSiNerInGroupInternal(oldgroup.Id.Value, sinner.Id.Value, user, _context, _logger, oldgroup.PasswordHash, roles); } } catch (DbUpdateConcurrencyException ex) { foreach (var entry in ex.Entries) { if (entry.Entity is SINner || entry.Entity is Tag) { try { Utils.DbUpdateConcurrencyExceptionHandler(entry, _logger); } catch (Exception e) { res = new ResultSinnerPostSIN(e); return(BadRequest(res)); } } else { var e = new NotSupportedException( "Don't know how to handle concurrency conflicts for " + entry.Metadata.Name); res = new ResultSinnerPostSIN(e); return(BadRequest(res)); } } } catch (DbUpdateException ex) { res = new ResultSinnerPostSIN(ex); return(BadRequest(res)); } catch (Exception e) { try { var tc = new Microsoft.ApplicationInsights.TelemetryClient(); Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry telemetry = new Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry(e); telemetry.Properties.Add("User", user?.Email); telemetry.Properties.Add("SINnerId", sinner?.Id?.ToString()); tc.TrackException(telemetry); } catch (Exception ex) { _logger.LogError(ex.ToString()); } res = new ResultSinnerPostSIN(e); return(Conflict(res)); } } List <Guid> myids = (from a in uploadInfo.SINners select a.Id.Value).ToList(); List <SINner> sinlist = new List <SINner>(); foreach (var id in myids) { var sin = from a in _context.SINners where a.Id == id select a; if (sin.Any()) { sinlist.Add(sin.FirstOrDefault()); } } res = new ResultSinnerPostSIN(sinlist); switch (returncode) { case HttpStatusCode.OK: return(Accepted(res)); case HttpStatusCode.Created: return(Created("SINnerPostSIN", res)); default: return(Ok(res)); } } catch (Exception e) { try { var tc = new Microsoft.ApplicationInsights.TelemetryClient(); Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry telemetry = new Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry(e); telemetry.Properties.Add("User", user?.Email); telemetry.Properties.Add("SINnerId", sinner?.Id?.ToString()); tc.TrackException(telemetry); } catch (Exception ex) { _logger.LogError(ex.ToString()); } res = new ResultSinnerPostSIN(e); return(BadRequest(res)); } }
public async Task <bool> UploadInBackground() { try { using (new CursorWait(true)) { this.PopulateTags(); await this.PrepareModel(); var res = await ChummerHub.Client.Backend.Utils.PostSINnerAsync(this); var response = await Backend.Utils.HandleError(res) as ResultBase; if (response?.CallSuccess == true) { try { var jsonResultString = res.Response.Content.ReadAsStringAsync().Result; try { ResultSinnerPostSIN objIds = Newtonsoft.Json.JsonConvert.DeserializeObject <ResultSinnerPostSIN>(jsonResultString); this.MySINnerFile.Id = objIds.MySINners.FirstOrDefault().Id; } catch (Exception ex) { Log.Error(ex); throw; } Log.Debug("Character " + this.MyCharacter.Alias + " posted with ID " + this.MySINnerFile.Id); await ChummerHub.Client.Backend.Utils.UploadChummerFileAsync(this).ContinueWith((uploadtask) => { if (uploadtask.Status != TaskStatus.RanToCompletion) { if (uploadtask.Exception != null) { throw uploadtask.Exception; } return; } if (uploadtask.Result?.Response?.StatusCode != HttpStatusCode.OK) { Log.Warn("Upload failed for Character " + this.MyCharacter.Alias + ": " + uploadtask.Result?.Response?.StatusCode); } else { Log.Trace("Character " + this.MyCharacter.Alias + " uploaded with Id " + this.MySINnerFile.Id); } }); } catch (Exception ex) { Log.Error(ex); throw; } } } } catch (Exception e) { Log.Error(e); MessageBox.Show(e.ToString()); } return(true); }