├── .gitattributes
├── .gitignore
├── README.md
├── SpeedUpCoreAPIExample.sln
└── SpeedUpCoreAPIExample
├── Contexts
└── DefaultContext.cs
├── Controllers
├── PricesController.cs
└── ProductsController.cs
├── Docs
└── Speed up ASP.NET API app.Part1.pdf
├── Interfaces
├── IPricesRepository.cs
├── IPricesService.cs
├── IProductsRepository.cs
└── IProductsService.cs
├── Models
├── Price.cs
└── Product.cs
├── Program.cs
├── Properties
└── launchSettings.json
├── Repositories
├── PricesRepository.cs
└── ProductsRepository.cs
├── SQL
└── SpeedUpCoreAPIExampleDB.sql
├── Services
├── PricesService.cs
└── ProductsService.cs
├── SpeedUpCoreAPIExample.csproj
├── Startup.cs
├── ViewModels
├── PriceViewModel.cs
└── ProductViewModel.cs
├── appsettings.Development.json
└── appsettings.json
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.userosscache
8 | *.sln.docstates
9 |
10 | # User-specific files (MonoDevelop/Xamarin Studio)
11 | *.userprefs
12 |
13 | # Build results
14 | [Dd]ebug/
15 | [Dd]ebugPublic/
16 | [Rr]elease/
17 | [Rr]eleases/
18 | x64/
19 | x86/
20 | bld/
21 | [Bb]in/
22 | [Oo]bj/
23 | [Ll]og/
24 |
25 | # Visual Studio 2015 cache/options directory
26 | .vs/
27 | # Uncomment if you have tasks that create the project's static files in wwwroot
28 | #wwwroot/
29 |
30 | # MSTest test Results
31 | [Tt]est[Rr]esult*/
32 | [Bb]uild[Ll]og.*
33 |
34 | # NUNIT
35 | *.VisualState.xml
36 | TestResult.xml
37 |
38 | # Build Results of an ATL Project
39 | [Dd]ebugPS/
40 | [Rr]eleasePS/
41 | dlldata.c
42 |
43 | # DNX
44 | project.lock.json
45 | project.fragment.lock.json
46 | artifacts/
47 |
48 | *_i.c
49 | *_p.c
50 | *_i.h
51 | *.ilk
52 | *.meta
53 | *.obj
54 | *.pch
55 | *.pdb
56 | *.pgc
57 | *.pgd
58 | *.rsp
59 | *.sbr
60 | *.tlb
61 | *.tli
62 | *.tlh
63 | *.tmp
64 | *.tmp_proj
65 | *.log
66 | *.vspscc
67 | *.vssscc
68 | .builds
69 | *.pidb
70 | *.svclog
71 | *.scc
72 |
73 | # Chutzpah Test files
74 | _Chutzpah*
75 |
76 | # Visual C++ cache files
77 | ipch/
78 | *.aps
79 | *.ncb
80 | *.opendb
81 | *.opensdf
82 | *.sdf
83 | *.cachefile
84 | *.VC.db
85 | *.VC.VC.opendb
86 |
87 | # Visual Studio profiler
88 | *.psess
89 | *.vsp
90 | *.vspx
91 | *.sap
92 |
93 | # TFS 2012 Local Workspace
94 | $tf/
95 |
96 | # Guidance Automation Toolkit
97 | *.gpState
98 |
99 | # ReSharper is a .NET coding add-in
100 | _ReSharper*/
101 | *.[Rr]e[Ss]harper
102 | *.DotSettings.user
103 |
104 | # JustCode is a .NET coding add-in
105 | .JustCode
106 |
107 | # TeamCity is a build add-in
108 | _TeamCity*
109 |
110 | # DotCover is a Code Coverage Tool
111 | *.dotCover
112 |
113 | # NCrunch
114 | _NCrunch_*
115 | .*crunch*.local.xml
116 | nCrunchTemp_*
117 |
118 | # MightyMoose
119 | *.mm.*
120 | AutoTest.Net/
121 |
122 | # Web workbench (sass)
123 | .sass-cache/
124 |
125 | # Installshield output folder
126 | [Ee]xpress/
127 |
128 | # DocProject is a documentation generator add-in
129 | DocProject/buildhelp/
130 | DocProject/Help/*.HxT
131 | DocProject/Help/*.HxC
132 | DocProject/Help/*.hhc
133 | DocProject/Help/*.hhk
134 | DocProject/Help/*.hhp
135 | DocProject/Help/Html2
136 | DocProject/Help/html
137 |
138 | # Click-Once directory
139 | publish/
140 |
141 | # Publish Web Output
142 | *.[Pp]ublish.xml
143 | *.azurePubxml
144 | # TODO: Comment the next line if you want to checkin your web deploy settings
145 | # but database connection strings (with potential passwords) will be unencrypted
146 | #*.pubxml
147 | *.publishproj
148 |
149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
150 | # checkin your Azure Web App publish settings, but sensitive information contained
151 | # in these scripts will be unencrypted
152 | PublishScripts/
153 |
154 | # NuGet Packages
155 | *.nupkg
156 | # The packages folder can be ignored because of Package Restore
157 | **/packages/*
158 | # except build/, which is used as an MSBuild target.
159 | !**/packages/build/
160 | # Uncomment if necessary however generally it will be regenerated when needed
161 | #!**/packages/repositories.config
162 | # NuGet v3's project.json files produces more ignoreable files
163 | *.nuget.props
164 | *.nuget.targets
165 |
166 | # Microsoft Azure Build Output
167 | csx/
168 | *.build.csdef
169 |
170 | # Microsoft Azure Emulator
171 | ecf/
172 | rcf/
173 |
174 | # Windows Store app package directories and files
175 | AppPackages/
176 | BundleArtifacts/
177 | Package.StoreAssociation.xml
178 | _pkginfo.txt
179 |
180 | # Visual Studio cache files
181 | # files ending in .cache can be ignored
182 | *.[Cc]ache
183 | # but keep track of directories ending in .cache
184 | !*.[Cc]ache/
185 |
186 | # Others
187 | ClientBin/
188 | ~$*
189 | *~
190 | *.dbmdl
191 | *.dbproj.schemaview
192 | *.jfm
193 | *.pfx
194 | *.publishsettings
195 | node_modules/
196 | orleans.codegen.cs
197 |
198 | # Since there are multiple workflows, uncomment next line to ignore bower_components
199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
200 | #bower_components/
201 |
202 | # RIA/Silverlight projects
203 | Generated_Code/
204 |
205 | # Backup & report files from converting an old project file
206 | # to a newer Visual Studio version. Backup files are not needed,
207 | # because we have git ;-)
208 | _UpgradeReport_Files/
209 | Backup*/
210 | UpgradeLog*.XML
211 | UpgradeLog*.htm
212 |
213 | # SQL Server files
214 | *.mdf
215 | *.ldf
216 |
217 | # Business Intelligence projects
218 | *.rdl.data
219 | *.bim.layout
220 | *.bim_*.settings
221 |
222 | # Microsoft Fakes
223 | FakesAssemblies/
224 |
225 | # GhostDoc plugin setting file
226 | *.GhostDoc.xml
227 |
228 | # Node.js Tools for Visual Studio
229 | .ntvs_analysis.dat
230 |
231 | # Visual Studio 6 build log
232 | *.plg
233 |
234 | # Visual Studio 6 workspace options file
235 | *.opt
236 |
237 | # Visual Studio LightSwitch build output
238 | **/*.HTMLClient/GeneratedArtifacts
239 | **/*.DesktopClient/GeneratedArtifacts
240 | **/*.DesktopClient/ModelManifest.xml
241 | **/*.Server/GeneratedArtifacts
242 | **/*.Server/ModelManifest.xml
243 | _Pvt_Extensions
244 |
245 | # Paket dependency manager
246 | .paket/paket.exe
247 | paket-files/
248 |
249 | # FAKE - F# Make
250 | .fake/
251 |
252 | # JetBrains Rider
253 | .idea/
254 | *.sln.iml
255 |
256 | # CodeRush
257 | .cr/
258 |
259 | # Python Tools for Visual Studio (PTVS)
260 | __pycache__/
261 | *.pyc
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Speed up ASP.NET Core WEB API application. Part 1
2 | Creating a test RESTful WEB API application.
3 |
4 | The article is divided into three parts:
5 |
6 | Part 1. Creating a test RESTful WEB API application: https://github.com/EduardSilantiev/Speed-up-ASP.NET-Core-WEB-API-app-Part1
7 | Part 2. Using various approaches to increase the application's productivity: https://github.com/EduardSilantiev/Speed-up-ASP.NET-Core-WEB-API-app-Part2
8 | Part 3. Deep refactoring and refinement of ASP.NET Core WEB API application code: https://github.com/EduardSilantiev/Speed-up-ASP.NET-Core-WEB-API-app-Part3
9 |
10 | In Part 1 we will create an asynchronous RESTful WEB API service that be able to search products in a database and get price lists of different suppliers for the particular product.
11 |
12 | Documentation for Part.1
13 | /SpeedUpCoreAPIExample/Docs/Speed up ASP.NET API app.Part1.pdf
14 |
15 | SQL code for Part.1
16 | /SpeedUpCoreAPIExample/SQL/
17 |
18 | Articles at Codeproject
19 | https://www.codeproject.com/Articles/1260600/Speed-Up-ASP-NET-Core-WEB-API-Application-Part-1
20 | https://www.codeproject.com/Articles/1261345/Speed-up-ASP-NET-Core-WEB-API-application-Part-2
21 | https://www.codeproject.com/Articles/4049519/Speed-up-ASP-NET-Core-WEB-API-application-Part-3
22 |
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27130.2036
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SpeedUpCoreAPIExample", "SpeedUpCoreAPIExample\SpeedUpCoreAPIExample.csproj", "{9512F37B-7D58-49D5-997B-3051D11AAEA6}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {9512F37B-7D58-49D5-997B-3051D11AAEA6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {9512F37B-7D58-49D5-997B-3051D11AAEA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {9512F37B-7D58-49D5-997B-3051D11AAEA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {9512F37B-7D58-49D5-997B-3051D11AAEA6}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {B77B6F7D-CAAA-4304-A7C8-B4C2E8CB65AD}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/Contexts/DefaultContext.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.EntityFrameworkCore;
2 | using SpeedUpCoreAPIExample.Models;
3 |
4 | namespace SpeedUpCoreAPIExample.Contexts
5 | {
6 | public class DefaultContext : DbContext
7 | {
8 | public virtual DbSet Products { get; set; }
9 | public virtual DbSet Prices { get; set; }
10 |
11 | public DefaultContext(DbContextOptions options) : base(options)
12 | {
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/Controllers/PricesController.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Mvc;
2 | using SpeedUpCoreAPIExample.Interfaces;
3 | using System.Threading.Tasks;
4 |
5 | namespace SpeedUpCoreAPIExample.Contexts
6 | {
7 | [Route("api/[controller]")]
8 | [ApiController]
9 | public class PricesController : ControllerBase
10 | {
11 | private readonly IPricesService _pricesService;
12 |
13 | public PricesController(IPricesService pricesService)
14 | {
15 | _pricesService = pricesService;
16 | }
17 |
18 | // GET /api/prices/1
19 | [HttpGet("{Id}")]
20 | public async Task GetPricesAsync(int id)
21 | {
22 | return await _pricesService.GetPricesAsync(id);
23 | }
24 | }
25 | }
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/Controllers/ProductsController.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Mvc;
2 | using SpeedUpCoreAPIExample.Interfaces;
3 | using System.Threading.Tasks;
4 |
5 | namespace SpeedUpCoreAPIExample.Controllers
6 | {
7 | [Route("api/[controller]")]
8 | [ApiController]
9 | public class ProductsController : Controller
10 | {
11 | private readonly IProductsService _productsService;
12 |
13 | public ProductsController(IProductsService productsService)
14 | {
15 | _productsService = productsService;
16 | }
17 |
18 | // GET /api/products
19 | [HttpGet]
20 | public async Task GetAllProductsAsync()
21 | {
22 | return await _productsService.GetAllProductsAsync();
23 | }
24 |
25 | // GET /api/products/5
26 | [HttpGet("{id}")]
27 | public async Task GetProductAsync(int id)
28 | {
29 | return await _productsService.GetProductAsync(id);
30 | }
31 |
32 | // GET /api/products/find
33 | [HttpGet("find/{sku}")]
34 | public async Task FindProductsAsync(string sku)
35 | {
36 | return await _productsService.FindProductsAsync(sku);
37 | }
38 |
39 | // DELETE /api/products/5
40 | [HttpDelete("{id}")]
41 | public async Task DeleteProductAsync(int id)
42 | {
43 | return await _productsService.DeleteProductAsync(id);
44 | }
45 | }
46 | }
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/Docs/Speed up ASP.NET API app.Part1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/EduardSilantiev/Speed-up-ASP.NET-Core-WEB-API-app-Part1/ee295b5d7182c681928e79ee5a29393be05b272c/SpeedUpCoreAPIExample/Docs/Speed up ASP.NET API app.Part1.pdf
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/Interfaces/IPricesRepository.cs:
--------------------------------------------------------------------------------
1 | using SpeedUpCoreAPIExample.Models;
2 | using System.Collections.Generic;
3 | using System.Threading.Tasks;
4 |
5 | namespace SpeedUpCoreAPIExample.Interfaces
6 | {
7 | public interface IPricesRepository
8 | {
9 | Task> GetPricesAsync(int productId);
10 | }
11 | }
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/Interfaces/IPricesService.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Mvc;
2 | using System.Threading.Tasks;
3 |
4 | namespace SpeedUpCoreAPIExample.Interfaces
5 | {
6 | public interface IPricesService
7 | {
8 | Task GetPricesAsync(int productId);
9 | }
10 | }
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/Interfaces/IProductsRepository.cs:
--------------------------------------------------------------------------------
1 | using SpeedUpCoreAPIExample.Models;
2 | using System.Collections.Generic;
3 | using System.Threading.Tasks;
4 |
5 | namespace SpeedUpCoreAPIExample.Interfaces
6 | {
7 | public interface IProductsRepository
8 | {
9 | Task> GetAllProductsAsync();
10 | Task GetProductAsync(int productId);
11 | Task> FindProductsAsync(string sku);
12 | Task DeleteProductAsync(int productId);
13 | }
14 | }
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/Interfaces/IProductsService.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Mvc;
2 | using System.Threading.Tasks;
3 |
4 | namespace SpeedUpCoreAPIExample.Interfaces
5 | {
6 | public interface IProductsService
7 | {
8 | Task GetAllProductsAsync();
9 | Task GetProductAsync(int productId);
10 | Task FindProductsAsync(string sku);
11 | Task DeleteProductAsync(int productId);
12 | }
13 | }
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/Models/Price.cs:
--------------------------------------------------------------------------------
1 | namespace SpeedUpCoreAPIExample.Models
2 | {
3 | public class Price
4 | {
5 | public int PriceId { get; set; }
6 | public int ProductId { get; set; }
7 | public decimal Value { get; set; }
8 | public string Supplier { get; set; }
9 | }
10 | }
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/Models/Product.cs:
--------------------------------------------------------------------------------
1 | namespace SpeedUpCoreAPIExample.Models
2 | {
3 | public class Product
4 | {
5 | public int ProductId { get; set; }
6 | public string Sku { get; set; }
7 | public string Name { get; set; }
8 | }
9 | }
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Threading.Tasks;
6 | using Microsoft.AspNetCore;
7 | using Microsoft.AspNetCore.Hosting;
8 | using Microsoft.Extensions.Configuration;
9 | using Microsoft.Extensions.Logging;
10 |
11 | namespace SpeedUpCoreAPIExample
12 | {
13 | public class Program
14 | {
15 | public static void Main(string[] args)
16 | {
17 | BuildWebHost(args).Run();
18 | }
19 |
20 | public static IWebHost BuildWebHost(string[] args) =>
21 | WebHost.CreateDefaultBuilder(args)
22 | .UseStartup()
23 | .Build();
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "iisSettings": {
3 | "windowsAuthentication": false,
4 | "anonymousAuthentication": true,
5 | "iisExpress": {
6 | "applicationUrl": "http://localhost:49858/",
7 | "sslPort": 0
8 | }
9 | },
10 | "profiles": {
11 | "IIS Express": {
12 | "commandName": "IISExpress",
13 | "launchBrowser": true,
14 | "launchUrl": "api",
15 | "environmentVariables": {
16 | "ASPNETCORE_ENVIRONMENT": "Development"
17 | }
18 | },
19 | "SpeedUpCoreAPIExample": {
20 | "commandName": "Project",
21 | "launchBrowser": true,
22 | "launchUrl": "api",
23 | "environmentVariables": {
24 | "ASPNETCORE_ENVIRONMENT": "Development"
25 | },
26 | "applicationUrl": "http://localhost:49858/"
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/Repositories/PricesRepository.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.EntityFrameworkCore;
2 | using SpeedUpCoreAPIExample.Contexts;
3 | using SpeedUpCoreAPIExample.Interfaces;
4 | using SpeedUpCoreAPIExample.Models;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Threading.Tasks;
8 |
9 | namespace SpeedUpCoreAPIExample.Repositories
10 | {
11 | public class PricesRepository : IPricesRepository
12 | {
13 | private readonly DefaultContext _context;
14 |
15 | public PricesRepository(DefaultContext context)
16 | {
17 | _context = context;
18 | }
19 |
20 | public async Task> GetPricesAsync(int productId)
21 | {
22 | return await _context.Prices.Where(p => p.ProductId == productId).ToListAsync();
23 | }
24 | }
25 | }
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/Repositories/ProductsRepository.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.EntityFrameworkCore;
2 | using SpeedUpCoreAPIExample.Contexts;
3 | using SpeedUpCoreAPIExample.Interfaces;
4 | using SpeedUpCoreAPIExample.Models;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Threading.Tasks;
8 |
9 | namespace SpeedUpCoreAPIExample.Repositories
10 | {
11 | public class ProductsRepository : IProductsRepository
12 | {
13 | private readonly DefaultContext _context;
14 |
15 | public ProductsRepository(DefaultContext context)
16 | {
17 | _context = context;
18 | }
19 |
20 | public async Task> GetAllProductsAsync()
21 | {
22 | return await _context.Products.ToListAsync();
23 | }
24 |
25 | public async Task GetProductAsync(int productId)
26 | {
27 | return await _context.Products.Where(p => p.ProductId == productId).FirstOrDefaultAsync();
28 | }
29 |
30 | public async Task> FindProductsAsync(string sku)
31 | {
32 | return await _context.Products.Where(p => p.Sku.Contains(sku)).ToListAsync();
33 | }
34 |
35 | public async Task DeleteProductAsync(int productId)
36 | {
37 | Product product = await GetProductAsync(productId);
38 |
39 | if (product != null)
40 | {
41 | _context.Products.Remove(product);
42 |
43 | await _context.SaveChangesAsync();
44 | }
45 |
46 | return product;
47 | }
48 | }
49 | }
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/SQL/SpeedUpCoreAPIExampleDB.sql:
--------------------------------------------------------------------------------
1 | USE [master]
2 | GO
3 |
4 | CREATE DATABASE [SpeedUpCoreAPIExampleDB]
5 | GO
6 |
7 | USE [SpeedUpCoreAPIExampleDB]
8 | GO
9 |
10 | CREATE TABLE [dbo].[Products] (
11 | [ProductId] INT IDENTITY (1, 1) NOT NULL,
12 | [SKU] NCHAR (50) NOT NULL,
13 | [Name] NCHAR (150) NOT NULL,
14 | CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED ([ProductId] ASC)
15 | );
16 | GO
17 |
18 | CREATE TABLE [dbo].[Prices] (
19 | [PriceId] INT IDENTITY (1, 1) NOT NULL,
20 | [ProductId] INT NOT NULL,
21 | [Value] DECIMAL (18, 2) NOT NULL,
22 | [Supplier] NCHAR (50) NOT NULL,
23 | CONSTRAINT [PK_Prices] PRIMARY KEY CLUSTERED ([PriceId] ASC)
24 | );
25 | GO
26 |
27 | ALTER TABLE [dbo].[Prices] WITH CHECK ADD CONSTRAINT [FK_Prices_Products] FOREIGN KEY([ProductId])
28 | REFERENCES [dbo].[Products] ([ProductId])
29 | ON DELETE CASCADE
30 | GO
31 | ALTER TABLE [dbo].[Prices] CHECK CONSTRAINT [FK_Prices_Products]
32 | GO
33 |
34 | INSERT INTO Products ([SKU], [Name]) VALUES ('aaa', 'Product1');
35 | INSERT INTO Products ([SKU], [Name]) VALUES ('aab', 'Product2');
36 | INSERT INTO Products ([SKU], [Name]) VALUES ('abc', 'Product3');
37 |
38 | INSERT INTO Prices ([ProductId], [Value], [Supplier]) VALUES (1, 100, 'Bosch');
39 | INSERT INTO Prices ([ProductId], [Value], [Supplier]) VALUES (1, 125, 'LG');
40 | INSERT INTO Prices ([ProductId], [Value], [Supplier]) VALUES (1, 130, 'Garmin');
41 |
42 | INSERT INTO Prices ([ProductId], [Value], [Supplier]) VALUES (2, 140, 'Bosch');
43 | INSERT INTO Prices ([ProductId], [Value], [Supplier]) VALUES (2, 145, 'LG');
44 | INSERT INTO Prices ([ProductId], [Value], [Supplier]) VALUES (2, 150, 'Garmin');
45 |
46 | INSERT INTO Prices ([ProductId], [Value], [Supplier]) VALUES (3, 160, 'Bosch');
47 | INSERT INTO Prices ([ProductId], [Value], [Supplier]) VALUES (3, 165, 'LG');
48 | INSERT INTO Prices ([ProductId], [Value], [Supplier]) VALUES (3, 170, 'Garmin');
49 |
50 | GO
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/Services/PricesService.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Mvc;
2 | using SpeedUpCoreAPIExample.Interfaces;
3 | using SpeedUpCoreAPIExample.Models;
4 | using SpeedUpCoreAPIExample.ViewModels;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Threading.Tasks;
8 |
9 | namespace SpeedUpCoreAPIExample.Services
10 | {
11 | public class PricesService : IPricesService
12 | {
13 | private readonly IPricesRepository _pricesRepository;
14 |
15 | public PricesService(IPricesRepository pricesRepository)
16 | {
17 | _pricesRepository = pricesRepository;
18 | }
19 |
20 | public async Task GetPricesAsync(int productId)
21 | {
22 | try
23 | {
24 | IEnumerable pricess = await _pricesRepository.GetPricesAsync(productId);
25 |
26 | if (pricess != null)
27 | {
28 | return new OkObjectResult(pricess.Select(p => new PriceViewModel()
29 | {
30 | Price = p.Value,
31 | Supplier = p.Supplier.Trim()
32 | }
33 | )
34 | .OrderBy(p => p.Price)
35 | .ThenBy(p => p.Supplier)
36 | );
37 | }
38 | else
39 | {
40 | return new NotFoundResult();
41 | }
42 | }
43 | catch
44 | {
45 | return new ConflictResult();
46 | }
47 | }
48 | }
49 | }
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/Services/ProductsService.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Mvc;
2 | using SpeedUpCoreAPIExample.Interfaces;
3 | using SpeedUpCoreAPIExample.Models;
4 | using SpeedUpCoreAPIExample.ViewModels;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Threading.Tasks;
8 |
9 | namespace SpeedUpCoreAPIExample.Services
10 | {
11 | public class ProductsService : IProductsService
12 | {
13 | private readonly IProductsRepository _productsRepository;
14 |
15 | public ProductsService(IProductsRepository productsRepository)
16 | {
17 | _productsRepository = productsRepository;
18 | }
19 |
20 | public async Task FindProductsAsync(string sku)
21 | {
22 | try
23 | {
24 | IEnumerable products = await _productsRepository.FindProductsAsync(sku);
25 |
26 | if (products != null)
27 | {
28 | return new OkObjectResult(products.Select(p => new ProductViewModel()
29 | {
30 | Id = p.ProductId,
31 | Sku = p.Sku.Trim(),
32 | Name = p.Name.Trim()
33 | }
34 | ));
35 | }
36 | else
37 | {
38 | return new NotFoundResult();
39 | }
40 | }
41 | catch
42 | {
43 | return new ConflictResult();
44 | }
45 | }
46 |
47 | public async Task GetAllProductsAsync()
48 | {
49 | try
50 | {
51 | IEnumerable products = await _productsRepository.GetAllProductsAsync();
52 |
53 | if (products != null)
54 | {
55 | return new OkObjectResult(products.Select(p => new ProductViewModel()
56 | {
57 | Id = p.ProductId,
58 | Sku = p.Sku.Trim(),
59 | Name = p.Name.Trim()
60 | }
61 | ));
62 | }
63 | else
64 | {
65 | return new NotFoundResult();
66 | }
67 | }
68 | catch
69 | {
70 | return new ConflictResult();
71 | }
72 | }
73 |
74 | public async Task GetProductAsync(int productId)
75 | {
76 | try
77 | {
78 | Product product = await _productsRepository.GetProductAsync(productId);
79 |
80 | if (product != null)
81 | {
82 | return new OkObjectResult(new ProductViewModel()
83 | {
84 | Id = product.ProductId,
85 | Sku = product.Sku.Trim(),
86 | Name = product.Name.Trim()
87 | });
88 | }
89 | else
90 | {
91 | return new NotFoundResult();
92 | }
93 | }
94 | catch
95 | {
96 | return new ConflictResult();
97 | }
98 | }
99 |
100 | public async Task DeleteProductAsync(int productId)
101 | {
102 | try
103 | {
104 | Product product = await _productsRepository.DeleteProductAsync(productId);
105 |
106 | if (product != null)
107 | {
108 | return new OkObjectResult(new ProductViewModel()
109 | {
110 | Id = product.ProductId,
111 | Sku = product.Sku.Trim(),
112 | Name = product.Name.Trim()
113 | });
114 | }
115 | else
116 | {
117 | return new NotFoundResult();
118 | }
119 | }
120 | catch
121 | {
122 | return new ConflictResult();
123 | }
124 | }
125 | }
126 | }
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/SpeedUpCoreAPIExample.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp2.0
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/Startup.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Builder;
2 | using Microsoft.AspNetCore.Hosting;
3 | using Microsoft.EntityFrameworkCore;
4 | using Microsoft.Extensions.Configuration;
5 | using Microsoft.Extensions.DependencyInjection;
6 | using SpeedUpCoreAPIExample.Contexts;
7 | using SpeedUpCoreAPIExample.Interfaces;
8 | using SpeedUpCoreAPIExample.Repositories;
9 | using SpeedUpCoreAPIExample.Services;
10 |
11 | namespace SpeedUpCoreAPIExample
12 | {
13 | public class Startup
14 | {
15 | public Startup(IConfiguration configuration)
16 | {
17 | Configuration = configuration;
18 | }
19 |
20 | public IConfiguration Configuration { get; }
21 |
22 | // This method gets called by the runtime. Use this method to add services to the container.
23 | public void ConfigureServices(IServiceCollection services)
24 | {
25 | services.AddMvc();
26 |
27 | services.AddDbContext(options =>
28 | options.UseSqlServer(Configuration.GetConnectionString("DefaultDatabase")));
29 |
30 | services.AddScoped();
31 | services.AddScoped();
32 |
33 | services.AddTransient();
34 | services.AddTransient();
35 | }
36 |
37 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
38 | public void Configure(IApplicationBuilder app, IHostingEnvironment env)
39 | {
40 | if (env.IsDevelopment())
41 | {
42 | app.UseDeveloperExceptionPage();
43 | }
44 |
45 | app.UseMvc();
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/ViewModels/PriceViewModel.cs:
--------------------------------------------------------------------------------
1 | namespace SpeedUpCoreAPIExample.ViewModels
2 | {
3 | public class PriceViewModel
4 | {
5 | public decimal Price { get; set; }
6 | public string Supplier { get; set; }
7 | }
8 | }
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/ViewModels/ProductViewModel.cs:
--------------------------------------------------------------------------------
1 | namespace SpeedUpCoreAPIExample.ViewModels
2 | {
3 | public class ProductViewModel
4 | {
5 | public int Id { get; set; }
6 | public string Sku { get; set; }
7 | public string Name { get; set; }
8 | }
9 | }
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "IncludeScopes": false,
4 | "LogLevel": {
5 | "Default": "Debug",
6 | "System": "Information",
7 | "Microsoft": "Information"
8 | }
9 | },
10 | "ConnectionStrings": {
11 | "DefaultDatabase": "Server=localhost;Database=SpeedUpCoreAPIExampleDB;Integrated Security=True;"
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/SpeedUpCoreAPIExample/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "IncludeScopes": false,
4 | "Debug": {
5 | "LogLevel": {
6 | "Default": "Warning"
7 | }
8 | },
9 | "Console": {
10 | "LogLevel": {
11 | "Default": "Warning"
12 | }
13 | },
14 | "ConnectionStrings": {
15 | "DefaultDatabase": "Server=localhost;Database=SpeedUpCoreAPIExampleDB;Integrated Security=True;"
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------