Skip to content

Commit 905e2d8

Browse files
committed
Uploading the project to the repository
1 parent b8713b4 commit 905e2d8

File tree

13 files changed

+537
-1
lines changed

13 files changed

+537
-1
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.EntityFrameworkCore;
8+
using InventoryService.Models;
9+
using Microsoft.AspNetCore.Authorization;
10+
11+
namespace InventoryService.Controllers
12+
{
13+
[Authorize]
14+
[Route("api/[controller]")]
15+
[ApiController]
16+
public class ProductsController : ControllerBase
17+
{
18+
private readonly InventoryContext _context;
19+
20+
public ProductsController(InventoryContext context)
21+
{
22+
_context = context;
23+
}
24+
25+
[HttpGet]
26+
public async Task<ActionResult<IEnumerable<Products>>> GetProducts(bool? inStock, int? skip, int? take)
27+
{
28+
var products = _context.Products.AsQueryable();
29+
30+
if (inStock != null) // Adds the condition to check availability
31+
{
32+
products = _context.Products.Where(i => i.AvailableQuantity > 0);
33+
}
34+
35+
if (skip != null)
36+
{
37+
products = products.Skip((int)skip);
38+
}
39+
40+
if (take != null)
41+
{
42+
products = products.Take((int)take);
43+
}
44+
45+
return await products.ToListAsync();
46+
}
47+
48+
// GET: api/Products/5
49+
[HttpGet("{id}")]
50+
public async Task<ActionResult<Products>> GetProducts(int id)
51+
{
52+
var products = await _context.Products.FindAsync(id);
53+
54+
if (products == null)
55+
{
56+
return NotFound();
57+
}
58+
59+
return products;
60+
}
61+
62+
// PUT: api/Products/5
63+
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
64+
// more details see https://aka.ms/RazorPagesCRUD.
65+
[HttpPut("{id}")]
66+
public async Task<IActionResult> PutProducts(int id, Products products)
67+
{
68+
if (id != products.ProductId)
69+
{
70+
return BadRequest();
71+
}
72+
73+
_context.Entry(products).State = EntityState.Modified;
74+
75+
try
76+
{
77+
await _context.SaveChangesAsync();
78+
}
79+
catch (DbUpdateConcurrencyException)
80+
{
81+
if (!ProductsExists(id))
82+
{
83+
return NotFound();
84+
}
85+
else
86+
{
87+
throw;
88+
}
89+
}
90+
91+
return NoContent();
92+
}
93+
94+
// POST: api/Products
95+
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
96+
// more details see https://aka.ms/RazorPagesCRUD.
97+
[HttpPost]
98+
public async Task<ActionResult<Products>> PostProducts(Products products)
99+
{
100+
_context.Products.Add(products);
101+
await _context.SaveChangesAsync();
102+
103+
return CreatedAtAction("GetProducts", new { id = products.ProductId }, products);
104+
}
105+
106+
// DELETE: api/Products/5
107+
[HttpDelete("{id}")]
108+
public async Task<ActionResult<Products>> DeleteProducts(int id)
109+
{
110+
var products = await _context.Products.FindAsync(id);
111+
if (products == null)
112+
{
113+
return NotFound();
114+
}
115+
116+
_context.Products.Remove(products);
117+
await _context.SaveChangesAsync();
118+
119+
return products;
120+
}
121+
122+
private bool ProductsExists(int id)
123+
{
124+
return _context.Products.Any(e => e.ProductId == id);
125+
}
126+
}
127+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using InventoryService.Models;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.IdentityModel.Tokens;
6+
using System;
7+
using System.IdentityModel.Tokens.Jwt;
8+
using System.Security.Claims;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
12+
namespace InventoryService.Controllers
13+
{
14+
[Route("api/[controller]")]
15+
[ApiController]
16+
public class TokenController : ControllerBase
17+
{
18+
public IConfiguration _configuration;
19+
private readonly InventoryContext _context;
20+
21+
public TokenController(IConfiguration config, InventoryContext context)
22+
{
23+
_configuration = config;
24+
_context = context;
25+
}
26+
27+
[HttpPost]
28+
public async Task<IActionResult> Post(UserInfo _userData)
29+
{
30+
31+
if (_userData != null && _userData.Email != null && _userData.Password != null)
32+
{
33+
var user = await GetUser(_userData.Email, _userData.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["Jwt: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("FirstName", user.FirstName),
44+
new Claim("LastName", user.LastName),
45+
new Claim("UserName", user.UserName),
46+
new Claim("Email", user.Email)
47+
};
48+
49+
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["Jwt:Key"]));
50+
51+
var signIn = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
52+
53+
var token = new JwtSecurityToken(_configuration["Jwt:Issuer"], _configuration["Jwt: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<UserInfo> GetUser(string email, string password)
69+
{
70+
return await _context.UserInfo.FirstOrDefaultAsync(u => u.Email == email && u.Password == password);
71+
}
72+
}
73+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<None Include="InventoryService.sln" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.0" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.0" />
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
15+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.0">
16+
<PrivateAssets>all</PrivateAssets>
17+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
18+
</PackageReference>
19+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.0" />
20+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.0" />
21+
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.6.0" />
22+
</ItemGroup>
23+
24+
25+
</Project>
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.29519.181
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InventoryService", "InventoryService.csproj", "{423BC25E-7BB3-43C2-9290-8CC45A9D59EF}"
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+
{423BC25E-7BB3-43C2-9290-8CC45A9D59EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{423BC25E-7BB3-43C2-9290-8CC45A9D59EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{423BC25E-7BB3-43C2-9290-8CC45A9D59EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{423BC25E-7BB3-43C2-9290-8CC45A9D59EF}.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 = {70B23190-2671-4BAC-8B0A-D726719DF37A}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.EntityFrameworkCore.Metadata;
4+
5+
namespace InventoryService.Models
6+
{
7+
public partial class InventoryContext : DbContext
8+
{
9+
public InventoryContext(DbContextOptions<InventoryContext> options)
10+
: base(options)
11+
{
12+
}
13+
14+
public virtual DbSet<Products> Products { get; set; }
15+
public virtual DbSet<UserInfo> UserInfo { get; set; }
16+
17+
protected override void OnModelCreating(ModelBuilder modelBuilder)
18+
{
19+
modelBuilder.Entity<Products>(entity =>
20+
{
21+
entity.HasKey(e => e.ProductId)
22+
.HasName("PK__Products__B40CC6CDFEF2D15E");
23+
24+
entity.Property(e => e.Category)
25+
.HasMaxLength(100)
26+
.IsUnicode(false);
27+
28+
entity.Property(e => e.Color)
29+
.HasMaxLength(20)
30+
.IsUnicode(false);
31+
32+
entity.Property(e => e.Name)
33+
.IsRequired()
34+
.HasMaxLength(100)
35+
.IsUnicode(false);
36+
37+
entity.Property(e => e.UnitPrice).HasColumnType("decimal(18, 0)");
38+
});
39+
40+
modelBuilder.Entity<UserInfo>(entity =>
41+
{
42+
entity.HasKey(e => e.UserId)
43+
.HasName("PK__UserInfo__1788CC4C1F5C1650");
44+
45+
entity.Property(e => e.CreatedDate)
46+
.HasColumnType("datetime")
47+
.HasDefaultValueSql("(getdate())");
48+
49+
entity.Property(e => e.Email)
50+
.IsRequired()
51+
.HasMaxLength(50)
52+
.IsUnicode(false);
53+
54+
entity.Property(e => e.FirstName)
55+
.IsRequired()
56+
.HasMaxLength(30)
57+
.IsUnicode(false);
58+
59+
entity.Property(e => e.LastName)
60+
.IsRequired()
61+
.HasMaxLength(30)
62+
.IsUnicode(false);
63+
64+
entity.Property(e => e.Password)
65+
.IsRequired()
66+
.HasMaxLength(20)
67+
.IsUnicode(false);
68+
69+
entity.Property(e => e.UserName)
70+
.IsRequired()
71+
.HasMaxLength(30)
72+
.IsUnicode(false);
73+
});
74+
75+
OnModelCreatingPartial(modelBuilder);
76+
}
77+
78+
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
79+
}
80+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace InventoryService.Models
5+
{
6+
public partial class Products
7+
{
8+
public int ProductId { get; set; }
9+
public string Name { get; set; }
10+
public string Category { get; set; }
11+
public string Color { get; set; }
12+
public decimal UnitPrice { get; set; }
13+
public int AvailableQuantity { get; set; }
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace InventoryService.Models
5+
{
6+
public partial class UserInfo
7+
{
8+
public int UserId { get; set; }
9+
public string FirstName { get; set; }
10+
public string LastName { get; set; }
11+
public string UserName { get; set; }
12+
public string Email { get; set; }
13+
public string Password { get; set; }
14+
public DateTime CreatedDate { get; set; }
15+
}
16+
}

InventoryService/Program.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace InventoryService
11+
{
12+
public class Program
13+
{
14+
public static void Main(string[] args)
15+
{
16+
CreateHostBuilder(args).Build().Run();
17+
}
18+
19+
public static IHostBuilder CreateHostBuilder(string[] args) =>
20+
Host.CreateDefaultBuilder(args)
21+
.ConfigureWebHostDefaults(webBuilder =>
22+
{
23+
webBuilder.UseStartup<Startup>();
24+
});
25+
}
26+
}

0 commit comments

Comments
 (0)