diff --git a/API/API.Test/API.Test.csproj b/API/API.Test/API.Test.csproj new file mode 100644 index 0000000..2c91b18 --- /dev/null +++ b/API/API.Test/API.Test.csproj @@ -0,0 +1,77 @@ + + + + + + Debug + AnyCPU + {84714616-89D7-4A5D-86D8-2DC395530FEB} + Library + Properties + API.Test + API.Test + v4.6.2 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 15.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.dll + + + ..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.SqlServer.dll + + + ..\packages\MSTest.TestFramework.1.4.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll + + + ..\packages\MSTest.TestFramework.1.4.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll + + + + + + + + + + + + + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + \ No newline at end of file diff --git a/API/API.Test/App.config b/API/API.Test/App.config new file mode 100644 index 0000000..2fb423e --- /dev/null +++ b/API/API.Test/App.config @@ -0,0 +1,13 @@ + + + + +
+ + + + + + + + \ No newline at end of file diff --git a/API/API.Test/Properties/AssemblyInfo.cs b/API/API.Test/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..bafedfa --- /dev/null +++ b/API/API.Test/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("API.Test")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("API.Test")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: Guid("84714616-89d7-4a5d-86d8-2dc395530feb")] + +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/API/API.Test/UnitTest1.cs b/API/API.Test/UnitTest1.cs new file mode 100644 index 0000000..6872ca9 --- /dev/null +++ b/API/API.Test/UnitTest1.cs @@ -0,0 +1,43 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Data.Entity.Core.EntityClient; +using System.Data.SqlClient; + +namespace API.Test { + [TestClass] + public class SqlConnectionBuilder { + [TestMethod] + public void Build() { + var serverName = "localhost\\SQLEXPRESS"; + var databaseName = "wgenerator"; + var user = "sa"; + var pass = "sa"; + + // Initialize the connection string builder for the + // underlying provider. + var sqlBuilder = + new SqlConnectionStringBuilder { + + // Set the properties for the data source. + DataSource = serverName, + InitialCatalog = databaseName, + IntegratedSecurity = true, + UserID= user, + Password = pass, + }; + + // Build the SqlConnection connection string. + var providerString = sqlBuilder.ToString(); + + + Console.WriteLine(providerString.ToString()); + + using (var conn = + new SqlConnection(providerString.ToString())) { + conn.Open(); + Console.WriteLine("Just testing the connection."); + conn.Close(); + } + } + } +} diff --git a/API/API.Test/packages.config b/API/API.Test/packages.config new file mode 100644 index 0000000..5f49372 --- /dev/null +++ b/API/API.Test/packages.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/API/API.sln b/API/API.sln index 47305ab..1dd5c95 100644 --- a/API/API.sln +++ b/API/API.sln @@ -5,6 +5,10 @@ VisualStudioVersion = 15.0.28307.489 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API", "API\API.csproj", "{27056FE2-5A34-44B2-9196-6F19C04F27CC}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API.Test", "API.Test\API.Test.csproj", "{84714616-89D7-4A5D-86D8-2DC395530FEB}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Backend", "Backend", "{5DD4FA9B-976C-4CDB-9E73-CF3485DBDD72}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,10 +19,18 @@ Global {27056FE2-5A34-44B2-9196-6F19C04F27CC}.Debug|Any CPU.Build.0 = Debug|Any CPU {27056FE2-5A34-44B2-9196-6F19C04F27CC}.Release|Any CPU.ActiveCfg = Release|Any CPU {27056FE2-5A34-44B2-9196-6F19C04F27CC}.Release|Any CPU.Build.0 = Release|Any CPU + {84714616-89D7-4A5D-86D8-2DC395530FEB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {84714616-89D7-4A5D-86D8-2DC395530FEB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {84714616-89D7-4A5D-86D8-2DC395530FEB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {84714616-89D7-4A5D-86D8-2DC395530FEB}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {27056FE2-5A34-44B2-9196-6F19C04F27CC} = {5DD4FA9B-976C-4CDB-9E73-CF3485DBDD72} + {84714616-89D7-4A5D-86D8-2DC395530FEB} = {5DD4FA9B-976C-4CDB-9E73-CF3485DBDD72} + EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {27650156-7B3F-4C0C-83D3-D13119DE083F} EndGlobalSection diff --git a/API/API/API.csproj b/API/API/API.csproj index 41f149d..f36a8ac 100644 --- a/API/API/API.csproj +++ b/API/API/API.csproj @@ -56,7 +56,25 @@ ..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.SqlServer.dll + + ..\packages\Microsoft.AspNet.OData.7.1.0\lib\net45\Microsoft.AspNet.OData.dll + + + ..\packages\Microsoft.Extensions.DependencyInjection.2.2.0\lib\net461\Microsoft.Extensions.DependencyInjection.dll + + + ..\packages\Microsoft.Extensions.DependencyInjection.Abstractions.2.2.0\lib\netstandard2.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll + + + ..\packages\Microsoft.OData.Core.7.5.4\lib\portable-net45+win8+wpa81\Microsoft.OData.Core.dll + + + ..\packages\Microsoft.OData.Edm.7.5.4\lib\portable-net45+win8+wpa81\Microsoft.OData.Edm.dll + + + ..\packages\Microsoft.Spatial.7.5.4\lib\portable-net45+win8+wpa81\Microsoft.Spatial.dll + ..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll @@ -66,6 +84,16 @@ ..\packages\Microsoft.AspNet.WebApi.Client.5.2.7\lib\net45\System.Net.Http.Formatting.dll + + ..\packages\System.Reflection.4.3.0\lib\net462\System.Reflection.dll + True + True + + + ..\packages\System.Runtime.Extensions.4.3.1\lib\net462\System.Runtime.Extensions.dll + True + True + @@ -98,9 +126,9 @@ - + - + @@ -122,7 +150,9 @@ - + + Designer + Web.config @@ -130,9 +160,7 @@ Web.config - - - + diff --git a/API/API/App_Start/Bootstrapper.cs b/API/API/App_Start/Bootstrapper.cs deleted file mode 100644 index 32eb596..0000000 --- a/API/API/App_Start/Bootstrapper.cs +++ /dev/null @@ -1,31 +0,0 @@ -using Autofac; -using Autofac.Integration.WebApi; -using System.Reflection; -using System.Web.Http; - -namespace API.App_Start -{ - /// - /// https://autofaccn.readthedocs.io/en/latest/integration/webapi.html#quick-start - /// - public class Bootstrapper - { - public void InitContainer() { - var config = GlobalConfiguration.Configuration; - var builder = new ContainerBuilder(); - builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); - builder.RegisterWebApiFilterProvider(config); - builder.RegisterWebApiModelBinderProvider(); - - RegisterServices(builder); - - var container = builder.Build(); - config.DependencyResolver = new AutofacWebApiDependencyResolver(container); - } - - private void RegisterServices(ContainerBuilder builder) { - // ... builder.RegisterType(); - } - - } -} \ No newline at end of file diff --git a/API/API/App_Start/DependencyInjectionConfig.cs b/API/API/App_Start/DependencyInjectionConfig.cs new file mode 100644 index 0000000..0594403 --- /dev/null +++ b/API/API/App_Start/DependencyInjectionConfig.cs @@ -0,0 +1,39 @@ +using API.Database; +using Autofac; +using Autofac.Integration.WebApi; +using ProductService.Controllers; +using System.Reflection; +using System.Web.Http; + +namespace API.App_Start { + /// + /// https://autofaccn.readthedocs.io/en/latest/integration/webapi.html#quick-start + /// + public static class DependencyInjectionConfig { + public static void RegisterDependecyInjection(this HttpConfiguration config) { + var builder = new ContainerBuilder(); + builder.RegisterControllers(); + builder.RegisterWebApiFilterProvider(config); + builder.RegisterWebApiModelBinderProvider(); + + builder.RegisterServices(); + builder.RegisterDataProvider(); + + var container = builder.Build(); + config.DependencyResolver = new AutofacWebApiDependencyResolver(container); + } + + private static void RegisterControllers(this ContainerBuilder builder) { + builder.RegisterType().InstancePerRequest(); + } + + private static void RegisterServices(this ContainerBuilder builder) { + // ... builder.RegisterType(); + } + + private static void RegisterDataProvider(this ContainerBuilder builder) { + builder.RegisterType().InstancePerRequest(); + } + + } +} \ No newline at end of file diff --git a/API/API/App_Start/WebApiConfig.cs b/API/API/App_Start/WebApiConfig.cs index c928640..4a3fb33 100644 --- a/API/API/App_Start/WebApiConfig.cs +++ b/API/API/App_Start/WebApiConfig.cs @@ -1,24 +1,34 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using API.App_Start; +using API.Database.Model; +using Microsoft.AspNet.OData.Builder; +using Microsoft.AspNet.OData.Extensions; +using System.Net.Http.Headers; using System.Web.Http; -namespace API -{ - public static class WebApiConfig - { - public static void Register(HttpConfiguration config) - { +namespace API { + public static class WebApiConfig { + public static void Register(HttpConfiguration config) { // Web API configuration and services + config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); // Web API routes config.MapHttpAttributeRoutes(); - config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); + + // Web API oData configuration + var builder = new ODataConventionModelBuilder(); + builder.EntitySet("songs"); + config.MapODataServiceRoute( + routeName: "songs", + routePrefix: "odata", + model: builder.GetEdmModel()); + + config.RegisterDependecyInjection(); + } } } diff --git a/API/API/Controllers/SongsController.cs b/API/API/Controllers/SongsController.cs new file mode 100644 index 0000000..ce72cb7 --- /dev/null +++ b/API/API/Controllers/SongsController.cs @@ -0,0 +1,78 @@ +using API.Database; +using API.Database.Model; +using Microsoft.AspNet.OData; +using System.Data.Entity; +using System.Data.Entity.Infrastructure; +using System.Linq; +using System.Threading.Tasks; +using System.Web.Http; +namespace ProductService.Controllers { + public class SongsController : ODataController { + + public SongsController(DataContext db) { + this.db = db; + } + + private readonly DataContext db; + + private bool ProductExists(int key) { + return db.Songs.Any(p => p.ID == key); + } + + [EnableQuery] + public IQueryable Get() { + return db.Songs; + } + + [EnableQuery] + public SingleResult Get([FromODataUri] int key) { + var result = db.Songs.Where(p => p.ID == key); + return SingleResult.Create(result); + } + + public async Task Post(Song song) { + if (!ModelState.IsValid) return BadRequest(ModelState); + + db.Songs.Add(song); + await db.SaveChangesAsync(); + return Created(song); + } + + public async Task Patch([FromODataUri] int key, Delta product) { + if (!ModelState.IsValid) { + return BadRequest(ModelState); + } + var entity = await db.Songs.FindAsync(key); + if (entity == null) return NotFound(); + + product.Patch(entity); + try { + await db.SaveChangesAsync(); + } catch (DbUpdateConcurrencyException) { + if (!ProductExists(key)) return NotFound(); + + throw; + + } + return Updated(entity); + } + + public async Task Put([FromODataUri] int key, Song update) { + if (!ModelState.IsValid) return BadRequest(ModelState); + if (key != update.ID) return BadRequest(); + + db.Entry(update).State = EntityState.Modified; + try { + await db.SaveChangesAsync(); + } catch (DbUpdateConcurrencyException) { + if (!ProductExists(key)) return NotFound(); + + throw; + + } + return Updated(update); + } + + + } +} \ No newline at end of file diff --git a/API/API/Controllers/ValuesController.cs b/API/API/Controllers/ValuesController.cs deleted file mode 100644 index 634904b..0000000 --- a/API/API/Controllers/ValuesController.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Web.Http; - -namespace API.Controllers -{ - public class ValuesController : ApiController - { - // GET api/values - public IEnumerable Get() - { - return new string[] { "value1", "value2" }; - } - - // GET api/values/5 - public string Get(int id) - { - return "value"; - } - - // POST api/values - public void Post([FromBody]string value) - { - } - - // PUT api/values/5 - public void Put(int id, [FromBody]string value) - { - } - - // DELETE api/values/5 - public void Delete(int id) - { - } - } -} diff --git a/API/API/Web.config b/API/API/Web.config index ef71ee2..d940b1d 100644 --- a/API/API/Web.config +++ b/API/API/Web.config @@ -8,6 +8,9 @@
+ + + @@ -61,6 +64,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/API/API/packages.config b/API/API/packages.config index b154579..cd44b51 100644 --- a/API/API/packages.config +++ b/API/API/packages.config @@ -4,10 +4,28 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file