file detail edit api

This commit is contained in:
2019-03-28 15:46:52 +01:00
parent d9f2724101
commit cccdfb4a44
7 changed files with 111 additions and 81 deletions

View File

@@ -1,39 +1,24 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<configuration> <configuration>
<appSettings> <appSettings>
<add key="url" value="http://localhost/API" /> <!--<add key="url" value="http://localhost/API" />-->
<!--<add key="url" value="http://test.benjamin-ifland.de"/>--> <add key="url" value="http://test.benjamin-ifland.de"/>
</appSettings> </appSettings>
<runtime> <runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly>
<assemblyIdentity name="Microsoft.OData.Core" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<dependentAssembly> <bindingRedirect oldVersion="0.0.0.0-7.5.4.30215" newVersion="7.5.4.30215" />
</dependentAssembly>
<assemblyIdentity name="Microsoft.OData.Core" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <dependentAssembly>
<assemblyIdentity name="Microsoft.OData.Edm" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-7.5.4.30215" newVersion="7.5.4.30215" /> <bindingRedirect oldVersion="0.0.0.0-7.5.4.30215" newVersion="7.5.4.30215" />
</dependentAssembly>
</dependentAssembly> <dependentAssembly>
<assemblyIdentity name="Microsoft.Spatial" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<dependentAssembly> <bindingRedirect oldVersion="0.0.0.0-7.5.4.30215" newVersion="7.5.4.30215" />
</dependentAssembly>
<assemblyIdentity name="Microsoft.OData.Edm" publicKeyToken="31bf3856ad364e35" culture="neutral" /> </assemblyBinding>
</runtime>
<bindingRedirect oldVersion="0.0.0.0-7.5.4.30215" newVersion="7.5.4.30215" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Spatial" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-7.5.4.30215" newVersion="7.5.4.30215" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration> </configuration>

View File

@@ -1,10 +1,11 @@
using System; using API.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Simple.OData.Client;
using System;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using API.Models; using File = System.IO.File;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Simple.OData.Client;
namespace API.Client.Test { namespace API.Client.Test {
[TestClass] [TestClass]
@@ -12,7 +13,7 @@ namespace API.Client.Test {
private const string TEST_CASE_IDENTIFIER = "TESTCASE"; private const string TEST_CASE_IDENTIFIER = "TESTCASE";
private static Song testSong; private static Song testSong;
[ClassInitialize] [ClassInitialize]
public static async Task TestInitialize(TestContext tc) { public static async Task TestInitialize(TestContext tc) {
@@ -39,7 +40,7 @@ namespace API.Client.Test {
var client = new Client(); var client = new Client();
var data = await client.Get(); var data = await client.Get();
Assert.IsTrue(data.Any(_=>_.ID == testSong.ID)); Assert.IsTrue(data.Any(_ => _.ID == testSong.ID));
} }
[TestMethod] [TestMethod]
@@ -64,6 +65,29 @@ namespace API.Client.Test {
Assert.AreEqual(newSong.Name, insertedSong.Name); Assert.AreEqual(newSong.Name, insertedSong.Name);
} }
[TestMethod]
public async Task PatchItem() {
var client = new Client();
var newSong = new Song {
Name = Guid.NewGuid().ToString(),
Text = TEST_CASE_IDENTIFIER,
Comments = "Item to Patch"
};
var response = await client.Post(newSong);
try {
var patchedSong = await client.Patch(response.ID, new { Comments = "patched" });
} catch (WebRequestException ex) {
File.WriteAllText("error.html", ex.Response);
throw;
}
var insertedSong = await client.Get(response.ID);
Assert.AreEqual("patched", insertedSong.Comments);
Assert.AreEqual(newSong.Name, insertedSong.Name);
Assert.AreEqual(TEST_CASE_IDENTIFIER, insertedSong.Text);
}
[TestMethod] [TestMethod]
public async Task DeleteItem() { public async Task DeleteItem() {
var client = new Client(); var client = new Client();
@@ -75,9 +99,14 @@ namespace API.Client.Test {
var response = await client.Post(newSong); var response = await client.Post(newSong);
var insertedSong = await client.Get(response.ID); var insertedSong = await client.Get(response.ID);
Assert.AreEqual(newSong.Name, insertedSong.Name); Assert.AreEqual(newSong.Name, insertedSong.Name);
try {
var count = await client.Delete(response.ID); var count = await client.Delete(response.ID);
Assert.AreEqual(1, count); Assert.AreEqual(1, count);
} catch (WebRequestException ex) {
File.WriteAllText("error.html", ex.Response);
throw;
}
try { try {
await client.Get(response.ID); await client.Get(response.ID);

View File

@@ -35,6 +35,12 @@ namespace API.Client {
return insertedSong; return insertedSong;
} }
public async Task<Song> Patch(long id, object value) {
var client = new ODataClient(url).For<Song>().Key(id);
var insertedSong = await client.Set(value).UpdateEntryAsync();
return insertedSong;
}
public async Task<int> Delete(long id) { public async Task<int> Delete(long id) {
var client = new ODataClient(url).For<Song>(); var client = new ODataClient(url).For<Song>();
var count = await client.Key(id).DeleteEntriesAsync(); var count = await client.Key(id).DeleteEntriesAsync();

View File

@@ -179,7 +179,6 @@
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="App_Data\environment.json" />
<None Include="packages.config" /> <None Include="packages.config" />
<None Include="Properties\PublishProfiles\deploy to test.pubxml" /> <None Include="Properties\PublishProfiles\deploy to test.pubxml" />
</ItemGroup> </ItemGroup>
@@ -194,7 +193,9 @@
<DependentUpon>201903172028249_Log.cs</DependentUpon> <DependentUpon>201903172028249_Log.cs</DependentUpon>
</EmbeddedResource> </EmbeddedResource>
</ItemGroup> </ItemGroup>
<ItemGroup /> <ItemGroup>
<Folder Include="App_Data\" />
</ItemGroup>
<PropertyGroup> <PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion> <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

View File

@@ -1,4 +1,5 @@
using API.Database; using API.Database;
using API.Models;
using API.Services; using API.Services;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
@@ -27,22 +28,18 @@ namespace API.Controllers {
} }
// GET: api/Files/5 // GET: api/Files/5
[Route("api/songs/{songId}/files/{fileId}")] [Route("api/songs/{songId}/files/{fileId}", Order = 1)]
public HttpResponseMessage Get(long songId, long fileId) { public async Task<HttpResponseMessage> Get(long songId, long fileId) {
var response = new HttpResponseMessage(HttpStatusCode.OK); var response = new HttpResponseMessage(HttpStatusCode.OK);
fileGarbageCollectionService.Collect(dataPath); fileGarbageCollectionService.Collect(dataPath);
var file = dataContext.Files.Find(fileId); var file = await dataContext.Files.FindAsync(fileId);
if (file == null || file.Song.ID != songId) return new HttpResponseMessage(HttpStatusCode.NotFound); if (file == null || file.Song.ID != songId) return new HttpResponseMessage(HttpStatusCode.NotFound);
var reference = file.ReferenceFile; var reference = file.ReferenceFile;
var filename = file.Name;
var stream = fileService.Load(dataPath, reference.ToString()); var stream = fileService.Load(dataPath, reference.ToString());
response.Content = new StreamContent(stream); response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") {
FileName = filename
};
return response; return response;
} }
@@ -65,7 +62,7 @@ namespace API.Controllers {
var filename = await fileService.Save(stream, dataPath, c); var filename = await fileService.Save(stream, dataPath, c);
// attach file reference to song // attach file reference to song
song.Files.Add(new Models.File { song.Files.Add(new File {
Name = file.FileName, Name = file.FileName,
ReferenceFile = filename ReferenceFile = filename
}); });
@@ -74,9 +71,29 @@ namespace API.Controllers {
return new HttpResponseMessage(HttpStatusCode.OK); return new HttpResponseMessage(HttpStatusCode.OK);
} }
[Route("api/songs/{songId}/files/{fileId}/edit"), HttpGet]
public async Task<HttpResponseMessage> Edit(long songId, long fileId, string Name, FileType FileType, CancellationToken c) {
var file = await dataContext.Files.FindAsync(fileId);
if (file == null || file.Song.ID != songId) return new HttpResponseMessage(HttpStatusCode.NotFound);
file.Name = Name;
file.FileType = FileType;
await dataContext.SaveChangesAsync();
return new HttpResponseMessage(HttpStatusCode.OK);
}
[Route("api/songs/{songId}/files/{fileId}/delete"), HttpGet]
public async Task<HttpResponseMessage> Delete(long songId, long fileId, CancellationToken c) {
var file = await dataContext.Files.FindAsync(fileId);
if (file == null || file.Song.ID != songId) return new HttpResponseMessage(HttpStatusCode.NotFound);
dataContext.Files.Remove(file);
await dataContext.SaveChangesAsync();
fileGarbageCollectionService.Collect(dataPath);
return new HttpResponseMessage(HttpStatusCode.OK);
}
//// PUT: api/Files/5 //// PUT: api/Files/5

View File

@@ -30,8 +30,15 @@
<remove name="OPTIONSVerbHandler"/> <remove name="OPTIONSVerbHandler"/>
<remove name="TRACEVerbHandler"/> <remove name="TRACEVerbHandler"/>
<remove name="WebDAV"/> <remove name="WebDAV"/>
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/> <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
</handlers> </handlers>
<security>
<requestFiltering>
<verbs>
<add verb="PATCH" allowed="true" />
</verbs>
</requestFiltering>
</security>
</system.webServer> </system.webServer>
<runtime> <runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

View File

@@ -6,34 +6,19 @@
</appSettings> </appSettings>
<runtime> <runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly>
<assemblyIdentity name="Microsoft.OData.Core" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<dependentAssembly> <bindingRedirect oldVersion="0.0.0.0-7.5.4.30215" newVersion="7.5.4.30215" />
</dependentAssembly>
<assemblyIdentity name="Microsoft.OData.Core" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <dependentAssembly>
<assemblyIdentity name="Microsoft.OData.Edm" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-7.5.4.30215" newVersion="7.5.4.30215" /> <bindingRedirect oldVersion="0.0.0.0-7.5.4.30215" newVersion="7.5.4.30215" />
</dependentAssembly>
</dependentAssembly> <dependentAssembly>
<assemblyIdentity name="Microsoft.Spatial" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<dependentAssembly> <bindingRedirect oldVersion="0.0.0.0-7.5.4.30215" newVersion="7.5.4.30215" />
</dependentAssembly>
<assemblyIdentity name="Microsoft.OData.Edm" publicKeyToken="31bf3856ad364e35" culture="neutral" /> </assemblyBinding>
</runtime>
<bindingRedirect oldVersion="0.0.0.0-7.5.4.30215" newVersion="7.5.4.30215" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Spatial" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-7.5.4.30215" newVersion="7.5.4.30215" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration> </configuration>