Skip to content

Commit

Permalink
Milestone step for Azure#285: Capability to upload via file or text i…
Browse files Browse the repository at this point in the history
…nput, and remove samples.
  • Loading branch information
Baas-hub authored and chidozieononiwu committed Sep 26, 2022
1 parent 606684d commit 761a60e
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 102 deletions.
47 changes: 0 additions & 47 deletions src/dotnet/APIView/APIViewWeb/Controllers/UsageSampleController.cs

This file was deleted.

14 changes: 14 additions & 0 deletions src/dotnet/APIView/APIViewWeb/Models/UsageSampleFileModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;

namespace APIViewWeb
{
public class UsageSampleFileModel
{
public string UsageSampleFileId { get; set; } = IdHelper.GenerateId();

public DateTime CreationDate { get; set; } = DateTime.Now;
}
}
27 changes: 6 additions & 21 deletions src/dotnet/APIView/APIViewWeb/Models/UsageSampleModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,14 @@ namespace APIViewWeb
public class UsageSampleModel
{
[JsonProperty("id")]
public string SampleId { get; set; }
public string SampleId { get; set; } = IdHelper.GenerateId();
public string ReviewId { get; set; }
public string UsageSampleFileId { get; set; }

public string SampleContent { get; set; }

public string UsageSampleFileId { get; set; } = IdHelper.GenerateId();

public UsageSampleModel(string revId, Stream sampleContent)
{
SampleId = revId;
if (sampleContent != null)
{
String content = sampleContent.ToString(); // Write to file later.
SampleContent = parseMDtoHTML(content);
}
else
{
SampleContent = null;
}
}

private string parseMDtoHTML(string md)
public UsageSampleModel(string revId, string fileId)
{
return md;
ReviewId = revId;
UsageSampleFileId = fileId;
}

}
Expand Down
20 changes: 20 additions & 0 deletions src/dotnet/APIView/APIViewWeb/Models/UsageSampleUploadModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace APIViewWeb.Pages.Assemblies
{
public class UsageSampleUploadModel
{
[BindProperty]
public string sampleString { get; set; }

[BindProperty]
public string ReviewId { get; set; }

[BindProperty]
public IFormFile File { get; set; }
}
}
23 changes: 16 additions & 7 deletions src/dotnet/APIView/APIViewWeb/Pages/Assemblies/Samples.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="mx-5 mt-2">
<div class="row">
<div class="col p-2" data-review-id=@Model.Review.ReviewId>
@if (Model.Sample.SampleContent == null)
@if (Model.Sample == null || Model.SampleContent == "No Sample.")
{
<div class="text-muted">There is no sample present in the review.
<br /> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#uploadModel">Add Usage Sample</button>
Expand All @@ -17,7 +17,15 @@
else
{
<div class="border rounded">
<p> There is a sample but you haven't coded this yet </p>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#uploadModel">Replace Usage Sample</button>
<form asp-page-handler="Upload" method="post">
<input asp-for="Upload.sampleString" hidden value="No Sample." />
<input asp-for="Upload.ReviewId" hidden value="@Model.Review.ReviewId" />
<button type="submit" name="submit" value="Submit" class="btn btn-primary">Remove Usage Sample</button>
</form>
</div>
<div class="border rounded">
<p> @Model.SampleContent </p>
</div>
}
</div>
Expand Down Expand Up @@ -53,21 +61,22 @@
<div class="tab-content border rounded p-3">
<div class="tab-pane active" id="md-text">
<div class="new-comment-content">
<form data-post-update="usagesamples" class="new-thread-comment-form" method="post" asp-controller="UsageSample" asp-action="Add">
<input name="reviewId" hidden value="@Model.Review.ReviewId"/>
<form asp-page-handler="Upload" method="post">
<div class="new-sample-content">
<textarea class="new-thread-comment-text form-control" name="sampleText" rows="5" placeholder="Enter your markdown formatted usage sample here"></textarea>
<textarea asp-for="Upload.sampleString" class="new-thread-comment-text form-control" rows="5" placeholder="Enter your markdown formatted usage sample here"></textarea>
</div>
<input asp-for="Upload.ReviewId" hidden value="@Model.Review.ReviewId" />
<button type="submit" name="submit" value="Submit" class="comment-submit-button btn btn-outline-dark">Add Sample</button>
</form>
</div>
</div>
<div class="tab-pane" id="md-file">
<form asp-page-handler="Upload" method="post" enctype="multipart/form-data">
<input asp-for="Upload.ReviewId" hidden value="@Model.Review.ReviewId" />
<div class="form-group">
<div class="custom-file">
<input asp-for="Upload.Files" type="file" class="custom-file-input">
<label asp-for="Upload.Files" class="custom-file-label">Select usage sample for this review</label>
<input asp-for="Upload.File" type="file" class="custom-file-input">
<label asp-for="Upload.File" class="custom-file-label">Select usage sample for this review</label>
</div>
</div>
<div class="modal-footer">
Expand Down
15 changes: 11 additions & 4 deletions src/dotnet/APIView/APIViewWeb/Pages/Assemblies/Samples.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class UsageSamplePageModel : PageModel
public string Endpoint { get; }
public ReviewModel Review { get; private set; }
public UsageSampleModel Sample { get; private set; }
public string NewSampleContent { get; set; }
public string SampleContent { get; set; }

public UsageSamplePageModel(
IConfiguration configuration,
Expand All @@ -32,13 +32,14 @@ public UsageSamplePageModel(
}

[FromForm]
public UploadModel Upload { get; set; }
public UsageSampleUploadModel Upload { get; set; }

public async Task<IActionResult> OnGetAsync(string id)
{
TempData["Page"] = "samples";
Review = await _reviewManager.GetReviewAsync(User, id);
Sample = await _samplesManager.GetReviewUsageSampleAsync(id);
SampleContent = await _samplesManager.GetUsageSampleContentAsync(Sample.UsageSampleFileId);
return Page();
}

Expand All @@ -49,16 +50,22 @@ public async Task<IActionResult> OnPostUploadAsync()
return RedirectToPage();
}

var file = Upload.Files.SingleOrDefault();
var file = Upload.File;
var sampleString = Upload.sampleString;
var reviewId = Upload.ReviewId;

if (file != null)
{
using (var openReadStream = file.OpenReadStream())
{
var reviewModel = await _samplesManager.CreateReviewUsageSampleAsync(Review.ReviewId, openReadStream);
await _samplesManager.CreateReviewUsageSampleAsync(reviewId, openReadStream);
return RedirectToPage();
}
}
else if (sampleString != null)
{
await _samplesManager.CreateReviewUsageSampleAsync(reviewId, sampleString);
}

return RedirectToPage();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.IO;
using System.Threading.Tasks;
using Azure.Storage.Blobs;
using Microsoft.Extensions.Configuration;
using System.Text;

namespace APIViewWeb
{
public class BlobUsageSampleRepository
{
private BlobContainerClient _container;

public BlobUsageSampleRepository(IConfiguration configuration)
{
var connectionString = configuration["Blob:ConnectionString"];
_container = new BlobContainerClient(connectionString, "usagesamples");
}

public async Task<Stream> GetUsageSampleAsync(string sampleFileId)
{
try
{
var info = await GetBlobClient(sampleFileId).DownloadAsync();
return info.Value.Content;
}
catch
{
await UploadUsageSampleAsync(sampleFileId, new MemoryStream(Encoding.UTF8.GetBytes("No Sample.")));
var info = await GetBlobClient(sampleFileId).DownloadAsync();
return info.Value.Content;
}
}

private BlobClient GetBlobClient(string sampleFileId)
{
return _container.GetBlobClient(sampleFileId);
}

public async Task UploadUsageSampleAsync(string sampleFileId, Stream stream)
{
await GetBlobClient(sampleFileId).UploadAsync(stream, overwrite:true);
}

public async Task DeleteUsageSampleAsync (string sampleFileId)
{
await GetBlobClient(sampleFileId).DeleteAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,42 @@ public class CosmosUsageSampleRepository
public CosmosUsageSampleRepository(IConfiguration configuration)
{
var client = new CosmosClient(configuration["Cosmos:ConnectionString"]);
_samplesContainer = client.GetContainer("APIView", "usagesamples");
_samplesContainer = client.GetContainer("APIView", "UsageSamples");
}

public async Task<UsageSampleModel> GetUsageSampleAsync(string sampleId)
public async Task<UsageSampleModel> GetUsageSampleAsync(string reviewId)
{
return await _samplesContainer.ReadItemAsync<UsageSampleModel>(sampleId, new PartitionKey(sampleId));
return await GetUsageSamplesFromQueryAsync($"SELECT * FROM UsageSamples c WHERE c.ReviewId = '{reviewId}'", reviewId);
}

public async Task DeleteUsageSampleAsync(UsageSampleModel Sample)
{
await _samplesContainer.DeleteItemAsync<UsageSampleModel>(Sample.SampleId, new PartitionKey(Sample.SampleId));
await _samplesContainer.DeleteItemAsync<UsageSampleModel>(Sample.SampleId, new PartitionKey(Sample.ReviewId));
}

public async Task UpsertUsageSampleAsync(UsageSampleModel sampleModel)
{
await _samplesContainer.UpsertItemAsync(sampleModel, new PartitionKey(sampleModel.SampleId));
await _samplesContainer.UpsertItemAsync(sampleModel, new PartitionKey(sampleModel.ReviewId));
}

private async Task<UsageSampleModel> GetUsageSamplesFromQueryAsync(string query, string reviewId)
{
var itemQueryIterator = _samplesContainer.GetItemQueryIterator<UsageSampleModel>(query);
var result = await itemQueryIterator.ReadNextAsync();
try
{
return result.Resource.First();
}
catch
{
var file = new UsageSampleFileModel();
var sample = new UsageSampleModel(reviewId, file.UsageSampleFileId);
await _samplesContainer.UpsertItemAsync(sample);

itemQueryIterator = _samplesContainer.GetItemQueryIterator<UsageSampleModel>(query);
var newResult = await itemQueryIterator.ReadNextAsync();
return newResult.Resource.First();
}
}

}
Expand Down
Loading

0 comments on commit 761a60e

Please sign in to comment.