Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 17dcd3c

Browse files
author
RahulMule
committed
Added Delete Method
1 parent 6c40594 commit 17dcd3c

File tree

6 files changed

+65
-3
lines changed

6 files changed

+65
-3
lines changed

‎DotNetCore-WebAPI-AzureRedisCache/Controllers/ProductsController.cs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,36 @@ public Product AddProducts(Product product)
3838
{
3939
return _product.AddProduct(product);
4040
}
41+
42+
[HttpGet("{id}")]
43+
public async Task<ActionResult<Product>> GetProductbyID(int id)
44+
{
45+
var product = await _cache.GetCacheData<Product>($"Product{id}") ;
46+
if (product != null)
47+
{
48+
return Ok(product);
49+
}
50+
var prd = _product.GetProductbyID(id);
51+
if (prd != null)
52+
{
53+
var expiry = DateTime.Now.AddMinutes(5);
54+
await _cache.SetCacheData($"Product{id}",prd.Value,expiry);
55+
return Ok(prd.Value);
56+
}
57+
return NotFound();
58+
}
59+
60+
[HttpDelete("{id}")]
61+
public async Task<ActionResult> DeleteProduct(int id)
62+
{
63+
var resp = await _product.DeleteProduct(id);
64+
await _cache.RemoveData($"Product{id}");
65+
if(resp == null)
66+
{
67+
return NotFound();
68+
}
69+
return NoContent();
70+
71+
}
4172
}
4273
}

‎DotNetCore-WebAPI-AzureRedisCache/DotNetCore-WebAPI-AzureRedisCache.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
1112
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.21" />
1213
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.21" />
1314
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using DotNetCore_WebAPI_AzureRedisCache.Models;
2+
using Microsoft.AspNetCore.Mvc;
23

34
namespace DotNetCore_WebAPI_AzureRedisCache.IRepository
45
{
56
public interface IProduct
67
{
78
public List<Product> GetProducts();
89
public Product AddProduct(Product product);
10+
public ActionResult<Product> GetProductbyID(int id);
11+
public Task<ActionResult> DeleteProduct(int id);
912
}
1013
}

‎DotNetCore-WebAPI-AzureRedisCache/Repository/AzureRedisCache.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ async Task<T> ICache.GetCacheData<T>(string key)
2121
return default;
2222
}
2323

24-
Task<object> ICache.RemoveData(string key)
24+
asyncTask<object> ICache.RemoveData(string key)
2525
{
26-
throw new NotImplementedException();
26+
var resp = await _database.KeyDeleteAsync(key);
27+
return resp;
2728
}
2829

2930
async Task<bool> ICache.SetCacheData<T>(string key, T value, DateTimeOffset expirationtime)

‎DotNetCore-WebAPI-AzureRedisCache/Repository/ProductRepository.cs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using DotNetCore_WebAPI_AzureRedisCache.IRepository;
22
using DotNetCore_WebAPI_AzureRedisCache.Models;
33
using DotNetCore_WebAPI_AzureRedisCache.ProductContext;
4+
using Microsoft.AspNetCore.Mvc;
45

56
namespace DotNetCore_WebAPI_AzureRedisCache.Repository
67
{
@@ -22,5 +23,30 @@ public Product AddProduct(Product product)
2223
_context.SaveChanges();
2324
return product;
2425
}
26+
27+
28+
29+
ActionResult<Product> IProduct.GetProductbyID(int id)
30+
{
31+
var product = _context.Products.FirstOrDefault(x => x.Id == id);
32+
if (product == null)
33+
{
34+
return default;
35+
}
36+
return product;
37+
}
38+
39+
async Task<ActionResult> IProduct.DeleteProduct(int id)
40+
{
41+
var product = await _context.Products.FindAsync(id);
42+
if (product == null)
43+
{
44+
return null;
45+
}
46+
_context.Products.Remove(product);
47+
await _context.SaveChangesAsync();
48+
return new NoContentResult();
49+
50+
}
2551
}
2652
}

‎DotNetCore-WebAPI-AzureRedisCache/appsettings.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
},
88
"AllowedHosts": "*",
99
"ConnectionStrings": {
10-
"connstring": "testazurecache.redis.cache.windows.net:6380,password=unoWPylldyw7kk4X6faRdHBWovc2UVRgUAzCaKaZfQ8=,ssl=True,abortConnect=False"
10+
"connstring": "sdasda.redis.cache.windows.net:6380,password=QKieBdeqRuZaXc83MlbYFGLCz88YDYg9pAzCaEgKYXQ=,ssl=True,abortConnect=False"
1111
}
1212
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /