Skip to content

Commit 42b731f

Browse files
committed
working on authenthification
1 parent 0bad4d6 commit 42b731f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1216
-73
lines changed
+10-22
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
using AutoMapper;
2-
using Microsoft.AspNetCore.Mvc;
3-
using Microsoft.EntityFrameworkCore;
4-
using WebAPI.Entities;
1+
using Microsoft.AspNetCore.Mvc;
52
using WebAPI.Models;
63
using WebAPI.Serivces;
74

85
namespace WebAPI.Controllers
96
{
107
[Route("api/restaurant")]
8+
[ApiController]
119
public class RestaurantController : ControllerBase
1210
{
13-
11+
1412
private readonly IRestauranServices _restaurantService;
1513

1614
public RestaurantController(IRestauranServices restauranServices)
@@ -21,45 +19,35 @@ public RestaurantController(IRestauranServices restauranServices)
2119
public ActionResult<IEnumerable<RestaurantDto>> GetAll()
2220
{
2321
var resutarants = _restaurantService.GetAll();
24-
return resutarants.Any() ? Ok(resutarants) : BadRequest();
22+
return resutarants.Any() ? Ok(resutarants) : BadRequest();
2523
}
2624

2725
[HttpGet("{id}")]
2826
public ActionResult<RestaurantDto> Get([FromRoute] int id)
2927
{
30-
var restaurant = _restaurantService.GetById(id);
31-
return restaurant == null ? NotFound() : Ok(restaurant);
28+
var restaurant = _restaurantService.GetById(id);
29+
return Ok(restaurant);
3230
}
3331

3432
[HttpPost]
3533
public ActionResult CreateRestaurant([FromBody] CreateRestuarantDto dto)
3634
{
37-
if (!ModelState.IsValid)
38-
{
39-
return BadRequest(ModelState);
40-
}
41-
4235
int id = _restaurantService.Create(dto);
4336
return Created($"/api/restuarant/{id}", "ok pomyslnie dodoano");
4437
}
4538
[HttpDelete("{id}")]
4639
public ActionResult Delete([FromRoute] int id)
4740
{
48-
bool flag = _restaurantService.Delete(id);
49-
50-
return flag ? NoContent() : NotFound();
41+
_restaurantService.Delete(id);
42+
return NoContent();
5143

5244
}
5345
[HttpPut]
5446
public ActionResult<RestaurantDto> UpdateRestaurant([FromBody] UpdateRestaurantDto dto)
5547
{
56-
if (!ModelState.IsValid)
57-
{
58-
return BadRequest(ModelState);
59-
}
60-
bool flag = _restaurantService.UpdateRestaurant(dto);
48+
_restaurantService.UpdateRestaurant(dto);
6149
var updatedRestaruant = _restaurantService.GetById(dto.Id);
62-
return flag ? Ok(updatedRestaruant) : BadRequest();
50+
return Ok(updatedRestaruant);
6351
}
6452
}
6553
}

WebAPI/Entities/Addres.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class Addres
1010

1111
public virtual Restaurant Restaurant { get; set; }
1212

13-
13+
1414

1515
}
1616
}

WebAPI/Entities/Restaurant.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ public class Restaurant
44
{
55

66
public int Id { get; set; }
7-
public string Name { get; set; }
7+
public string Name { get; set; }
88
public string Description { get; set; }
9-
public string Category { get; set; }
9+
public string Category { get; set; }
1010
public bool HasDelivery { get; set; }
1111
public string ContactEmail { get; set; }
1212

1313
public string ContactNumber { get; set; }
1414
public int AddresId { get; set; }
1515
public virtual Addres Addres { get; set; }
1616

17-
public virtual List<Dish> Dishes { get; set; }
17+
public virtual List<Dish> Dishes { get; set; }
1818

1919
}
2020
}

WebAPI/Entities/RestaurantDbContext.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,28 @@
22

33
namespace WebAPI.Entities
44
{
5-
public class RestaurantDbContext: DbContext
5+
public class RestaurantDbContext : DbContext
66
{
77
private string _connectionString = "Server=MACIEK\\SQLEXPRESS;Database=RestaurantDb;Trusted_Connection=True;TrustServerCertificate=true;";
88
public DbSet<Restaurant> Restaurants { get; set; }
99
public DbSet<Addres> Adresses { get; set; }
1010

1111
public DbSet<Dish> Dishes { get; set; }
1212

13+
public DbSet<User> Users { get; set; }
14+
15+
public DbSet<Role> Roles { get; set; }
16+
1317
protected override void OnModelCreating(ModelBuilder modelBuilder)
1418
{
1519
modelBuilder.Entity<Restaurant>()
1620
.Property(r => r.Name)
1721
.HasMaxLength(25);
22+
modelBuilder.Entity<User>()
23+
.Property(u => u.Email)
24+
.IsRequired();
25+
1826

19-
2027

2128
}
2229

WebAPI/Entities/Role.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace WebAPI.Entities
2+
{
3+
public class Role
4+
{
5+
public int Id { get; set; }
6+
public string Name { get; set; }
7+
}
8+
}

WebAPI/Entities/User.cs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace WebAPI.Entities
2+
{
3+
public class User
4+
{
5+
public int Id { get; set; }
6+
public string Name { get; set; }
7+
public string Email { get; set; }
8+
public string Password { get; set; }
9+
public virtual Role Role { get; set; }
10+
11+
}
12+
}

WebAPI/Exepction/NotFoundExeption.cs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace WebAPI.Exepction
2+
{
3+
public class NotFoundExeption : Exception
4+
{
5+
public NotFoundExeption(string massage ) : base(massage)
6+
{
7+
8+
}
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using WebAPI.Exepction;
2+
3+
namespace WebAPI.Middleware.cs
4+
{
5+
public class ErrorHandlingMiddlewarecs : IMiddleware
6+
{
7+
private readonly ILogger _logger;
8+
9+
public ErrorHandlingMiddlewarecs(ILogger<ErrorHandlingMiddlewarecs> logger)
10+
{
11+
_logger = logger;
12+
13+
}
14+
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
15+
{
16+
try
17+
{
18+
await next.Invoke(context);
19+
}
20+
catch (NotFoundExeption nfex)
21+
{
22+
_logger.LogError(nfex, "Source was not found");
23+
context.Response.StatusCode = 404;
24+
await context.Response.WriteAsync("Source was not found");
25+
}
26+
catch (Exception ex)
27+
{
28+
_logger.LogError(ex, ex.Message);
29+
context.Response.StatusCode = 500;
30+
await context.Response.WriteAsync("Something went wrong");
31+
}
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Diagnostics;
2+
3+
namespace WebAPI.Middleware.cs
4+
{
5+
public class RequestTimeMiddleware : IMiddleware
6+
{
7+
private readonly ILogger _logger;
8+
private readonly Stopwatch _stopwatch;
9+
10+
public RequestTimeMiddleware(ILogger<RequestTimeMiddleware> logger) {
11+
_logger = logger;
12+
_stopwatch = new Stopwatch();
13+
}
14+
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
15+
{
16+
_stopwatch.Start();
17+
await next.Invoke(context);
18+
_stopwatch.Stop();
19+
_logger.LogWarning($"To zapytanie trwało: {_stopwatch.ElapsedMilliseconds}ms ", context.Request.Path);
20+
}
21+
}
22+
}

WebAPI/Migrations/20230731122739_test.Designer.cs

+163
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)