public async Task <IActionResult> PostShareUrl([FromBody] ShareUrl shareUrl) { if (string.IsNullOrWhiteSpace(shareUrl.OsmUserId) == false && shareUrl.OsmUserId != User.Identity.Name) { return(BadRequest("You can't create a share as someone else!")); } var random = new Random(Guid.NewGuid().GetHashCode()); shareUrl.CreationDate = DateTime.Now; shareUrl.LastViewed = DateTime.Now; shareUrl.ViewsCount = 0; var id = GetRandomString(10, random); while (await _repository.GetUrlById(id) != null) { id = GetRandomString(10, random); } shareUrl.Id = id; await _repository.AddUrl(shareUrl); #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed UploadImagesIfNeeded(shareUrl); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed return(Ok(shareUrl)); }
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "application/json"; string UserID, ShareUrl; int shareinfoid; UserID = context.Request.Form["UserID"]; //context.Request.QueryString["UserID"]; // ShareUrl = context.Request.Form["ShareUrl"]; //context.Request.QueryString["ShareUrl"];// shareinfoid = int.Parse(weitang.StringExtensions.GetNumber(ShareUrl.Substring(ShareUrl.LastIndexOf('/') + 1)).ToString()); Pageinexinfo sharerindex = new Pageinexinfo(); int sharecounts; WechatEntities wechat = new WechatEntities(); sharecounts = wechat.ShareLogs.Where(a => a.userID == UserID && a.shareUrl == ShareUrl).ToList().Count; sharerindex = wechat.Pageinexinfo.Where(a => a.shareinfoID == shareinfoid).FirstOrDefault(); // string result = JsonConvert.SerializeObject(new ApiResponse { r = "1", d = new { sharenums = sharecounts, sharerequest = sharerindex is null ? 0 : sharerindex.SharesRequirements },
private async Task <IActionResult> GetUrlAsFile(string id, string format, ShareUrl shareUrl) { var bytes = await _dataContainerConverterService.ToAnyFormat(shareUrl.DataContainer, format); var restuls = new FileContentResult(bytes, new MediaTypeHeaderValue($"application/{format}")) { FileDownloadName = id + "." + format }; return(restuls); }
public void PostShareUrl_IncorrectUser_ShouldReturnBadRequest() { var url = new ShareUrl { OsmUserId = "1" }; _controller.SetupIdentity(null, "2"); var results = _controller.PostShareUrl(url).Result as BadRequestObjectResult; Assert.IsNotNull(results); }
public void DeleteShareUrl_ItemInDatabase_ShouldRemoveIt() { var shareUrl = new ShareUrl { Id = "42", OsmUserId = "42" }; _repository.GetUrlById(shareUrl.Id).Returns(shareUrl); _controller.SetupIdentity(null, shareUrl.OsmUserId); _controller.DeleteShareUrl(shareUrl.Id).Wait(); _repository.Received(1).Delete(shareUrl); }
public void DeleteShareUrl_ItemUserIdDoesNotMatchUSer_ShouldReturnBadRequest() { _controller.SetupIdentity(null, "1"); var shareUrl = new ShareUrl { Id = "11", OsmUserId = "11" }; _repository.GetUrlById(shareUrl.Id).Returns(shareUrl); var result = _controller.DeleteShareUrl(shareUrl.Id).Result as BadRequestObjectResult; Assert.IsNotNull(result); }
public void PutShareUrl_ItemNotInDatabase_ShouldReturnBadRequest() { var shareUrl = new ShareUrl { Id = "42", OsmUserId = "42" }; _repository.GetUrlById(shareUrl.Id).Returns((ShareUrl)null); var results = _controller.PutShareUrl(shareUrl.Id, shareUrl).Result as NotFoundResult; Assert.IsNotNull(results); _repository.DidNotReceive().Update(Arg.Any <ShareUrl>()); }
public void GetImageForShare_UrlInDatabase_ShouldCreateIt() { var siteUrl = new ShareUrl { Id = "1", DataContainer = new DataContainer() }; _repository.GetUrlById(siteUrl.Id).Returns(siteUrl); var results = _controller.GetImageForShare(siteUrl.Id).Result as FileContentResult; Assert.IsNotNull(results); }
public void PutShareUrl_ItemBelongsToUSer_ShouldUpdateIt() { var shareUrl = new ShareUrl { Id = "1", OsmUserId = "1" }; _repository.GetUrlById(shareUrl.Id).Returns(shareUrl); _controller.SetupIdentity(null, shareUrl.OsmUserId); var results = _controller.PutShareUrl(shareUrl.Id, shareUrl).Result as OkObjectResult; Assert.IsNotNull(results); _repository.Received(1).Update(Arg.Any <ShareUrl>()); }
public void PutShareUrl_ItemDoesNotBelongToUSer_ShouldReturnBadRequest() { var shareUrl = new ShareUrl { Id = "42", OsmUserId = "42" }; _repository.GetUrlById(shareUrl.Id).Returns(shareUrl); _controller.SetupIdentity(null, "1"); var results = _controller.PutShareUrl(shareUrl.Id, shareUrl).Result as BadRequestObjectResult; Assert.IsNotNull(results); _repository.DidNotReceive().Update(Arg.Any <ShareUrl>()); }
public void PostShareUrl_WithImageDataUrl_ShouldAddShareUrlUploadToImgurAndUpdateIt() { // first fetch from repository returns an item while the second one doesn't _repository.GetUrlById(Arg.Any <string>()) .Returns(x => new ShareUrl(), x => null as ShareUrl); var url = "url"; var task = Task.Run(() => url); _imgurGateway.UploadImage(Arg.Any <Stream>()).Returns(task); var shareUrl = new ShareUrl { DataContainer = new DataContainer { Routes = new List <RouteData> { new RouteData { Markers = new List <MarkerData> { new MarkerData { Urls = new List <LinkData> { new LinkData { Url = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//" + "8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" } } } } } } } }; var results = _controller.PostShareUrl(shareUrl).Result as OkObjectResult; Assert.IsNotNull(results); var content = results.Value as ShareUrl; Assert.IsNotNull(results); Assert.IsNotNull(content); Assert.AreEqual(10, content.Id.Length); task.Wait(); Task.Delay(100).Wait(); // just to make sure the ContinueWith finishes to run... not a great workaround... _repository.Received(1).Update(Arg.Is <ShareUrl>(x => x.DataContainer.Routes.First().Markers.First().Urls.First().Url == url)); }
private Task UploadImagesIfNeeded(ShareUrl shareUrl) { var uploadTasks = new List <Task>(); var links = shareUrl.DataContainer?.Routes.SelectMany(r => r.Markers.SelectMany(m => m.Urls)); foreach (var link in links ?? new List <LinkData>()) { var file = _base64ImageConverter.ConvertToFile(link.Url); if (file == null) { continue; } _logger.LogInformation($"Uploading image to imgur for share: {shareUrl.Id}"); uploadTasks.Add(UploadToImgurAndUpdateLink(file, link)); } return(uploadTasks.Any() ? Task.WhenAll(uploadTasks).ContinueWith((t, a) => _repository.Update(shareUrl), null) : Task.CompletedTask); }
public async Task <IActionResult> PostShareUrl([FromBody] ShareUrl shareUrl) { if (string.IsNullOrWhiteSpace(shareUrl.OsmUserId) == false && shareUrl.OsmUserId != User.Identity.Name) { return(BadRequest("You can't create a share as someone else!")); } var random = new Random(Guid.NewGuid().GetHashCode()); shareUrl.CreationDate = DateTime.Now; shareUrl.LastViewed = DateTime.Now; shareUrl.ViewsCount = 0; var id = GetRandomString(10, random); while (await _repository.GetUrlById(id) != null) { id = GetRandomString(10, random); } shareUrl.Id = id; await _repository.AddUrl(shareUrl); return(Ok(shareUrl)); }
public async Task <IActionResult> PutShareUrl(string id, [FromBody] ShareUrl shareUrl) { var shareUrlFromDatabase = await _repository.GetUrlById(id); if (shareUrlFromDatabase == null) { return(NotFound()); } if (shareUrlFromDatabase.OsmUserId != User.Identity.Name) { return(BadRequest("You can't update someone else's share!")); } shareUrlFromDatabase.Title = shareUrl.Title; shareUrlFromDatabase.Description = shareUrl.Description; if (shareUrl.DataContainer != null) { // update can be made without the datacontainer data shareUrlFromDatabase.DataContainer = shareUrl.DataContainer; } await _repository.Update(shareUrlFromDatabase); return(Ok(shareUrlFromDatabase)); }
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "application/json"; Pageinexinfo sharerindex = new Pageinexinfo(); ShareLog mylog = new ShareLog(); int sharecounts, shareinfoid; string UserID, ShareUrl; try { UserID = context.Request.Form["UserID"]; //context.Request.QueryString["UserID"]; // ShareUrl = context.Request.Form["ShareUrl"]; //context.Request.QueryString["ShareUrl"];// shareinfoid = int.Parse(weitang.StringExtensions.GetNumber(ShareUrl.Substring(ShareUrl.LastIndexOf('/') + 1)).ToString()); mylog.userID = UserID; mylog.shareUrl = ShareUrl; mylog.shareinfoID = shareinfoid; mylog.BrowseType = DN.Framework.Utility.ClientHelper.GetUserAgent(); mylog.ClientIp = DN.Framework.Utility.ClientHelper.ClientIP(); mylog.IsMobile = DN.Framework.Utility.ClientHelper.GetIsMobileDevice() ? 1 : 0; mylog.OsName = DN.Framework.Utility.ClientHelper.GetOsName(); mylog.BrowseName = DN.Framework.Utility.ClientHelper.GetBrowseName(); mylog.BrowseVersion = DN.Framework.Utility.ClientHelper.GetBrowseVersion(); var ipinfo = DN.WeiAd.Business.Services.IpTaoBaoHelper.GetIpResult(mylog.ClientIp); if (ipinfo != null) { if (ipinfo.code == 0 && ipinfo.data != null) { mylog.Country = ipinfo.data.country; mylog.Area = ipinfo.data.area; mylog.City = ipinfo.data.city; mylog.Region = ipinfo.data.region; mylog.County = ipinfo.data.county; mylog.Isp = ipinfo.data.isp; } } mylog.InputDate = System.DateTime.Now; } catch (Exception ex) { DN.Framework.Utility.LogHelper.Write(ex.Message.ToString()); throw; } try { string cmd = string.Format(@" insert into ShareLog(userID, shareUrl, shareinfoID, BrowseType, ClientIp, IsMobile, OsName, BrowseName, BrowseVersion, Country, Area, City, Region, County, Isp,InputDate ) values (N'{0}',N'{1}',N'{2}',N'{3}',N'{4}',{5},N'{6}',N'{7}',N'{8}',N'{9}',N'{10}',N'{11}',N'{12}',N'{13}',N'{14}',N'{15}') ", mylog.userID, mylog.shareUrl, mylog.shareinfoID, mylog.BrowseType, mylog.ClientIp, mylog.IsMobile, mylog.OsName, mylog.BrowseName, mylog.BrowseVersion, mylog.Country, mylog.Area, mylog.City, mylog.Region, mylog.County, mylog.Isp, mylog.InputDate ); if (SQLServerHelper.ExcuteSql(cmd) > 0) { } } catch (Exception ex) { DN.Framework.Utility.LogHelper.Write(ex.Message); } WechatEntities wechat = new WechatEntities(); sharecounts = wechat.ShareLogs.Where(a => a.userID == UserID && a.shareUrl == ShareUrl).ToList().Count; sharerindex = wechat.Pageinexinfo.Where(a => a.shareinfoID == shareinfoid).FirstOrDefault(); // string result = JsonConvert.SerializeObject(new ApiResponse { r = "1", d = new { sharenums = sharecounts, sharerequest = sharerindex is null ? 0 : sharerindex.SharesRequirements, shareimages = sharerindex is null ? "" : sharerindex.friendimages },
public Task Update(ShareUrl shareUrl) { return(AddUrl(shareUrl)); }
public Task Delete(ShareUrl shareUrl) { return(_elasticClient.DeleteAsync <ShareUrl>(shareUrl.Id, d => d.Index(SHARES))); }
public Task AddUrl(ShareUrl shareUrl) { return(_elasticClient.IndexAsync(shareUrl, r => r.Index(SHARES).Id(shareUrl.Id))); }