Skip to content

Commit 00a6803

Browse files
Add project files.
1 parent 9f4dcfb commit 00a6803

21 files changed

+867
-0
lines changed

ProductCoreService_DOTNET5.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30717.126
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProductCoreService_DOTNET5", "ProductCoreService_DOTNET5\ProductCoreService_DOTNET5.csproj", "{78C91D65-55DD-45EE-8455-C38A4E9A879A}"
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+
{78C91D65-55DD-45EE-8455-C38A4E9A879A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{78C91D65-55DD-45EE-8455-C38A4E9A879A}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{78C91D65-55DD-45EE-8455-C38A4E9A879A}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{78C91D65-55DD-45EE-8455-C38A4E9A879A}.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 = {900E6043-80A9-4DFB-AD39-643EE15196E9}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Authorization;
6+
using Microsoft.AspNetCore.Http;
7+
using Microsoft.AspNetCore.Mvc;
8+
using Microsoft.EntityFrameworkCore;
9+
using ProductCoreService_DOTNET5.Models;
10+
11+
namespace ProductCoreService_DOTNET5.Controllers
12+
{
13+
[Route("api/[controller]")]
14+
[ApiController]
15+
[Authorize]
16+
public class ProductsController : ControllerBase
17+
{
18+
private readonly ProductContext _context;
19+
20+
public ProductsController(ProductContext context)
21+
{
22+
_context = context;
23+
}
24+
25+
// GET: api/Products
26+
[HttpGet]
27+
public async Task<ActionResult<IEnumerable<Product>>> GetProduct()
28+
{
29+
return await _context.Product.ToListAsync();
30+
}
31+
32+
// GET: api/Products/5
33+
[HttpGet("{id}")]
34+
public async Task<ActionResult<Product>> GetProduct(int id)
35+
{
36+
var product = await _context.Product.FindAsync(id);
37+
38+
if (product == null)
39+
{
40+
return NotFound();
41+
}
42+
43+
return product;
44+
}
45+
46+
// PUT: api/Products/5
47+
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
48+
[HttpPut("{id}")]
49+
public async Task<IActionResult> PutProduct(int id, Product product)
50+
{
51+
if (id != product.ProductId)
52+
{
53+
return BadRequest();
54+
}
55+
56+
_context.Entry(product).State = EntityState.Modified;
57+
58+
try
59+
{
60+
await _context.SaveChangesAsync();
61+
}
62+
catch (DbUpdateConcurrencyException)
63+
{
64+
if (!ProductExists(id))
65+
{
66+
return NotFound();
67+
}
68+
else
69+
{
70+
throw;
71+
}
72+
}
73+
74+
return NoContent();
75+
}
76+
77+
// POST: api/Products
78+
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
79+
[HttpPost]
80+
public async Task<ActionResult<Product>> PostProduct(Product product)
81+
{
82+
_context.Product.Add(product);
83+
await _context.SaveChangesAsync();
84+
85+
return CreatedAtAction("GetProduct", new { id = product.ProductId }, product);
86+
}
87+
88+
// DELETE: api/Products/5
89+
[HttpDelete("{id}")]
90+
public async Task<IActionResult> DeleteProduct(int id)
91+
{
92+
var product = await _context.Product.FindAsync(id);
93+
if (product == null)
94+
{
95+
return NotFound();
96+
}
97+
98+
_context.Product.Remove(product);
99+
await _context.SaveChangesAsync();
100+
101+
return NoContent();
102+
}
103+
104+
private bool ProductExists(int id)
105+
{
106+
return _context.Product.Any(e => e.ProductId == id);
107+
}
108+
}
109+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.Extensions.Configuration;
4+
using Microsoft.IdentityModel.Tokens;
5+
using ProductCoreService_DOTNET5.Models;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.IdentityModel.Tokens.Jwt;
9+
using System.Linq;
10+
using System.Security.Claims;
11+
using System.Text;
12+
using System.Threading.Tasks;
13+
14+
namespace ProductCoreService_DOTNET5.Controllers
15+
{
16+
[Route("api/[controller]")]
17+
[ApiController]
18+
public class TokenController : ControllerBase
19+
{
20+
public IConfiguration _configuration;
21+
private readonly ProductContext _context;
22+
public TokenController(IConfiguration config, ProductContext context)
23+
{
24+
_configuration = config;
25+
_context = context;
26+
}
27+
[HttpPost]
28+
public async Task<IActionResult> Post(User _user)
29+
{
30+
31+
if (_user != null && _user.UserName != null && _user.Password != null)
32+
{
33+
var user = await GetUser(_user.UserName, _user.Password);
34+
35+
if (user != null)
36+
{
37+
//create claims details based on the user information
38+
var claims = new[] {
39+
new Claim(JwtRegisteredClaimNames.Sub, _configuration["JwtConfig:Subject"]),
40+
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
41+
new Claim(JwtRegisteredClaimNames.Iat, DateTime.UtcNow.ToString()),
42+
new Claim("Id", user.UserId.ToString()),
43+
new Claim("FullName", user.FullName),
44+
new Claim("UserName", user.UserName),
45+
new Claim("Email", user.Email),
46+
new Claim("UserRole", user.UserRole)
47+
};
48+
49+
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JwtConfig:Key"]));
50+
51+
var signIn = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
52+
53+
var token = new JwtSecurityToken(_configuration["JwtConfig:Issuer"], _configuration["JwtConfig:Audience"], claims, expires: DateTime.UtcNow.AddDays(1), signingCredentials: signIn);
54+
55+
return Ok(new JwtSecurityTokenHandler().WriteToken(token));
56+
}
57+
else
58+
{
59+
return BadRequest("Invalid credentials");
60+
}
61+
}
62+
else
63+
{
64+
return BadRequest();
65+
}
66+
}
67+
68+
private async Task<User> GetUser(string username, string password)
69+
{
70+
return _context.User.FirstOrDefault(u => u.UserName == username && u.Password == password);
71+
}
72+
}
73+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.Logging;
4+
using Microsoft.Extensions.Options;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
10+
namespace ProductCoreService_DOTNET5.Controllers
11+
{
12+
[ApiController]
13+
[Route("[controller]")]
14+
public class WeatherForecastController : ControllerBase
15+
{
16+
private readonly IConfiguration _config;
17+
18+
private static readonly string[] Summaries = new[]
19+
{
20+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
21+
};
22+
private readonly ILogger<WeatherForecastController> _logger;
23+
24+
public WeatherForecastController(ILogger<WeatherForecastController> logger, IConfiguration config)
25+
{
26+
_config = config;
27+
_logger = logger;
28+
}
29+
30+
[HttpGet]
31+
public IEnumerable<WeatherForecast> Get()
32+
{
33+
var rng = new Random();
34+
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
35+
{
36+
Date = DateTime.Now.AddDays(index),
37+
TemperatureC = rng.Next(-20, 55),
38+
Summary = Summaries[rng.Next(Summaries.Length)]
39+
})
40+
.ToArray();
41+
}
42+
}
43+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.EntityFrameworkCore;
6+
using ProductCoreService_DOTNET5.Models;
7+
8+
public class ProductContext : DbContext
9+
{
10+
public ProductContext (DbContextOptions<ProductContext> options)
11+
: base(options)
12+
{
13+
}
14+
15+
public DbSet<ProductCoreService_DOTNET5.Models.Product> Product { get; set; }
16+
public DbSet<ProductCoreService_DOTNET5.Models.User> User { get; set; }
17+
}

ProductCoreService_DOTNET5/Migrations/20201125190001_Initial.Designer.cs

Lines changed: 81 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)