Skip to content

Commit b2349a4

Browse files
Merge pull request #56 from DiyorMarket/UpdateControllers
Update controllers
2 parents a4f0daf + 6bfe6a8 commit b2349a4

14 files changed

+524
-146
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
using Inflow.Api.Constants;
2+
using Inflow.Api.LoginModels;
3+
using Inflow.Domain.Entities;
4+
using Inflow.Domain.Intefaces.Services;
5+
using Inflow.Infrastructure;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.IdentityModel.Tokens;
8+
using System.IdentityModel.Tokens.Jwt;
9+
using System.Security.Claims;
10+
using System.Text;
11+
12+
namespace Inflow.Api.Controllers
13+
{
14+
15+
[Route("api/auth")]
16+
[ApiController]
17+
public class AuthenticationController : ControllerBase
18+
{
19+
private readonly InflowDbContext _context;
20+
private readonly IEmailSender _emailSender;
21+
public AuthenticationController(InflowDbContext context, IEmailSender emailSender)
22+
{
23+
_context = context;
24+
_emailSender = emailSender;
25+
}
26+
27+
[HttpPost("login")]
28+
public ActionResult<string> Login(LoginRequest request)
29+
{
30+
var user = Authenticate(request.Login, request.Password);
31+
32+
if (user is null)
33+
{
34+
return Unauthorized();
35+
}
36+
37+
if (!FindUser(request.Login, request.Password))
38+
{
39+
return Unauthorized();
40+
}
41+
42+
var securityKey = new SymmetricSecurityKey(
43+
Encoding.UTF8.GetBytes("anvarSekretKalitSozMalades"));
44+
var signingCredentials = new SigningCredentials(securityKey,
45+
SecurityAlgorithms.HmacSha256);
46+
47+
var claimsForToken = new List<Claim>();
48+
claimsForToken.Add(new Claim("sub", user.Phone));
49+
claimsForToken.Add(new Claim("name", user.Name));
50+
51+
var jwtSecurityToken = new JwtSecurityToken(
52+
"anvar-api",
53+
"anvar-mobile",
54+
claimsForToken,
55+
DateTime.UtcNow,
56+
DateTime.UtcNow.AddDays(5),
57+
signingCredentials);
58+
59+
var token = new JwtSecurityTokenHandler()
60+
.WriteToken(jwtSecurityToken);
61+
62+
return Ok(token);
63+
}
64+
65+
[HttpPost("register")]
66+
public ActionResult Register(RegisterRequest request)
67+
{
68+
var existingUser = FindUser(request.Login);
69+
if (existingUser != null)
70+
{
71+
return Conflict("User with this login already exists.");
72+
}
73+
74+
var user = new User
75+
{
76+
Login = request.Login,
77+
Password = request.Password,
78+
Name = request.FullName,
79+
Phone = request.Phone
80+
};
81+
82+
_context.Users.Add(user);
83+
84+
_context.SaveChanges();
85+
86+
_emailSender.SendEmail(request.Login, EmailConfigurations.Subject, EmailConfigurations.RegisterBody.Replace("{recipientName}", request.FullName));
87+
88+
var securityKey = new SymmetricSecurityKey(
89+
Encoding.UTF8.GetBytes("anvarSekretKalitSozMalades"));
90+
var signingCredentials = new SigningCredentials(securityKey,
91+
SecurityAlgorithms.HmacSha256);
92+
93+
var claimsForToken = new List<Claim>();
94+
claimsForToken.Add(new Claim("sub", user.Phone));
95+
claimsForToken.Add(new Claim("name", user.Name));
96+
97+
var jwtSecurityToken = new JwtSecurityToken(
98+
"anvar-api",
99+
"anvar-mobile",
100+
claimsForToken,
101+
DateTime.UtcNow,
102+
DateTime.UtcNow.AddDays(30),
103+
signingCredentials);
104+
105+
var token = new JwtSecurityTokenHandler()
106+
.WriteToken(jwtSecurityToken);
107+
108+
return Ok(token);
109+
}
110+
111+
private bool FindUser(string login, string password)
112+
{
113+
var user = _context.Users.FirstOrDefault(u => u.Login == login);
114+
115+
if (user is null || user.Password != password)
116+
{
117+
return false;
118+
}
119+
120+
//Send email afte Login
121+
_emailSender.SendEmail(user.Login, EmailConfigurations.Subject, EmailConfigurations.LoginBody.Replace("{recipientName}", user.Name));
122+
123+
return true;
124+
}
125+
126+
private User FindUser(string login)
127+
{
128+
return _context.Users.FirstOrDefault(u => u.Login == login);
129+
}
130+
131+
static User Authenticate(string login, string password)
132+
{
133+
return new User()
134+
{
135+
Login = login,
136+
Password = password,
137+
Name = "Anvar",
138+
Phone = "124123"
139+
};
140+
}
141+
}
142+
}
143+

Inflow.Api/Controllers/CategoriesController.cs

+84-37
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
using Inflow.Domain.DTOs.Category;
1+
using ClosedXML.Excel;
2+
using Inflow.Domain.DTOs.Category;
23
using Inflow.Domain.DTOs.Product;
34
using Inflow.Domain.Interfaces.Services;
45
using Inflow.Domain.ResourceParameters;
56
using Inflow.ResourceParameters;
67
using Microsoft.AspNetCore.Mvc;
8+
using Syncfusion.Drawing;
9+
using Syncfusion.Pdf;
10+
using Syncfusion.Pdf.Grid;
11+
using System.Data;
712

813
namespace Inflow.Controllers
914
{
15+
1016
[Route("api/categories")]
1117
[ApiController]
1218
//[Authorize]
@@ -38,34 +44,41 @@ public ActionResult<CategoryDto> Get(int id)
3844
return Ok(category);
3945
}
4046

41-
//[HttpGet("export")]
42-
//public ActionResult ExportCustomers()
43-
//{
44-
// var category = _categoryService.GetAllCategories();
47+
[HttpGet("export/xls")]
48+
public ActionResult ExportCustomers()
49+
{
50+
var categories = _categoryService.GetAllCategories();
51+
byte[] data = GenerateExcle(categories);
52+
53+
return File(data, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Categories.xlsx");
54+
}
55+
56+
[HttpGet("export/pdf")]
57+
public IActionResult CreatePDFDocument()
58+
{
59+
PdfDocument document = new PdfDocument();
60+
PdfPage page = document.Pages.Add();
61+
62+
PdfGrid pdfGrid = new PdfGrid();
4563

46-
// using XLWorkbook wb = new XLWorkbook();
47-
// var sheet1 = wb.AddWorksheet(GetCategoriesDataTable(category), "Categories");
64+
var categories = _categoryService.GetAllCategories();
65+
List<object> data = ConvertCategoriesToData(categories);
4866

49-
// sheet1.Column(1).Style.Font.FontColor = XLColor.Red;
67+
pdfGrid.DataSource = data;
5068

51-
// sheet1.Columns(2, 4).Style.Font.FontColor = XLColor.Blue;
69+
pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent1);
5270

53-
// sheet1.Row(1).CellsUsed().Style.Fill.BackgroundColor = XLColor.Black;
54-
// //sheet1.Row(1).Cells(1,3).Style.Fill.BackgroundColor = XLColor.Yellow;
55-
// sheet1.Row(1).Style.Font.FontColor = XLColor.White;
71+
pdfGrid.Draw(page, new PointF(10, 10));
5672

57-
// sheet1.Row(1).Style.Font.Bold = true;
58-
// sheet1.Row(1).Style.Font.Shadow = true;
59-
// sheet1.Row(1).Style.Font.Underline = XLFontUnderlineValues.Single;
60-
// sheet1.Row(1).Style.Font.VerticalAlignment = XLFontVerticalTextAlignmentValues.Superscript;
61-
// sheet1.Row(1).Style.Font.Italic = true;
73+
MemoryStream stream = new MemoryStream();
74+
document.Save(stream);
75+
stream.Position = 0;
6276

63-
// sheet1.Rows(2, 3).Style.Font.FontColor = XLColor.AshGrey;
77+
string contentType = "application/pdf";
78+
string fileName = "categories.pdf";
6479

65-
// using MemoryStream ms = new MemoryStream();
66-
// wb.SaveAs(ms);
67-
// return File(ms.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Categories.xlsx");
68-
//}
80+
return File(stream, contentType, fileName);
81+
}
6982

7083
[HttpGet("{id}/products")]
7184
public ActionResult<ProductDto> GetProductsByCategoryId(
@@ -106,21 +119,55 @@ public ActionResult Delete(int id)
106119

107120
return NoContent();
108121
}
122+
private static byte[] GenerateExcle(IEnumerable<CategoryDto> categoryDtos)
123+
{
124+
using XLWorkbook wb = new();
125+
var sheet1 = wb.AddWorksheet(GetCategoriesDataTable(categoryDtos), "Categories");
126+
127+
sheet1.Columns(1, 3).Style.Font.FontColor = XLColor.Black;
128+
sheet1.Row(1).CellsUsed().Style.Fill.BackgroundColor = XLColor.Black;
129+
sheet1.Row(1).Style.Font.FontColor = XLColor.White;
130+
131+
sheet1.Column(1).Width = 5;
132+
sheet1.Columns(2, 3).Width = 12;
133+
134+
sheet1.Row(1).Style.Font.FontSize = 15;
135+
sheet1.Row(1).Style.Font.Bold = true;
136+
sheet1.Row(1).Style.Font.Shadow = true;
137+
sheet1.Row(1).Style.Font.VerticalAlignment = XLFontVerticalTextAlignmentValues.Superscript;
138+
sheet1.Row(1).Style.Font.Italic = false;
139+
140+
using MemoryStream ms = new();
141+
wb.SaveAs(ms);
142+
143+
return ms.ToArray();
144+
}
145+
private List<object> ConvertCategoriesToData(IEnumerable<CategoryDto> categories)
146+
{
147+
List<object> data = new List<object>();
148+
149+
foreach (var category in categories)
150+
{
151+
data.Add(new { ID = category.Id, category.Name, category.NumberOfProducts });
152+
}
153+
154+
return data;
155+
}
156+
private static DataTable GetCategoriesDataTable(IEnumerable<CategoryDto> categories)
157+
{
158+
DataTable table = new DataTable();
159+
table.TableName = "Categories Data";
160+
table.Columns.Add("Id", typeof(int));
161+
table.Columns.Add("Name", typeof(string));
162+
table.Columns.Add("Number of Products", typeof(int));
163+
164+
foreach (var category in categories)
165+
{
166+
table.Rows.Add(category.Id, category.Name, category.NumberOfProducts);
167+
}
168+
169+
return table;
170+
}
109171

110-
//private DataTable GetCategoriesDataTable(IEnumerable<CategoryDto> categories)
111-
//{
112-
// DataTable table = new DataTable();
113-
// table.TableName = "Categories Data";
114-
// table.Columns.Add("Id", typeof(int));
115-
// table.Columns.Add("Name", typeof(string));
116-
// table.Columns.Add("NumberOfProducts", typeof(string));
117-
118-
// foreach (var category in categories)
119-
// {
120-
// table.Rows.Add(category.Id, category.Name);
121-
// }
122-
123-
// return table;
124-
//}
125172
}
126173
}

0 commit comments

Comments
 (0)