Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public TableDto TableToTableDto(Table table)
{
return new TableDto
{
RestaurantId = table.Restaurant.Id,
NumberOfSeats = table.NumberOfSeats
RestaurantId = table.RestaurantId,
NumberOfSeats = table.NumberOfSeats,
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public GetTablesDto TableToTableDto(Table table)
return new GetTablesDto
{
Id = table.Id,
RestaurantId = table.Restaurant.Id,
RestaurantId = table.RestaurantId,
NumberOfSeats = table.NumberOfSeats
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace TableBooking.Logic.Interfaces
public interface IBookingRepository : IGenericRepository<Booking>
{
public Task<IEnumerable<Booking>> GetAllBookingsForSpecificUserAsync(Guid userId);
public Task<Booking> GetBookingByIdForSpecificUserAsync(Guid bookingId, Guid userId);
public Task<Booking?> GetBookingByIdForSpecificUserAsync(Guid bookingId, Guid userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public BookingRepository(TableBookingContext context) : base(context)

public async Task<IEnumerable<Booking>> GetAllBookingsForSpecificUserAsync(Guid userId)
{
return await _objectSet.Where(x => x.User.Id.Equals(userId)).ToListAsync();
return await _objectSet.Where(x => x.Id.Equals(userId)).ToListAsync();
}

public async Task<Booking> GetBookingByIdForSpecificUserAsync(Guid bookingId, Guid userId)
public async Task<Booking?> GetBookingByIdForSpecificUserAsync(Guid bookingId, Guid userId)
{
return await _objectSet.FirstOrDefaultAsync(x => x.Id.Equals(bookingId) && x.User.Id.Equals(userId));
return await _objectSet.FirstOrDefaultAsync(x => x.Id.Equals(bookingId) && x.Id.Equals(userId));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public TableRepository(TableBookingContext context) : base(context)
public async Task<IEnumerable<Table>> GetTablesByRestaurantIdAsync(Guid restaurantId)
{
return await _objectSet
.Include(x => x.Restaurant)
.Where(x => x.Restaurant.Id.Equals(restaurantId))
.Include(x => x.RestaurantId)
.Where(x => x.RestaurantId.Equals(restaurantId))
.ToListAsync();
}
}
Expand Down
6 changes: 4 additions & 2 deletions TableBookingAPI/TableBooking.Model/Dtos/TableDtos/TableDto.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace TableBooking.Model.Dtos.TableDtos
using TableBooking.Model.Models;

namespace TableBooking.Model.Dtos.TableDtos
{
public class TableDto
public class TableDto : Entity
{
public int NumberOfSeats { get; set; }
public Guid RestaurantId { get; set; }
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace TableBooking.Model.Migrations
{
public partial class initialProperWithOldFixes : Migration
public partial class AppUserId : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
Expand Down Expand Up @@ -113,7 +113,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
Id = table.Column<Guid>(type: "uuid", nullable: false),
Date = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
DurationInMinutes = table.Column<int>(type: "integer", nullable: false),
UserId = table.Column<Guid>(type: "uuid", nullable: false),
AppUserId = table.Column<Guid>(type: "uuid", nullable: false),
TableId = table.Column<Guid>(type: "uuid", nullable: false)
},
constraints: table =>
Expand All @@ -126,22 +126,22 @@ protected override void Up(MigrationBuilder migrationBuilder)
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_Bookings_Users_UserId",
column: x => x.UserId,
name: "FK_Bookings_Users_AppUserId",
column: x => x.AppUserId,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});

migrationBuilder.CreateIndex(
name: "IX_Bookings_TableId",
name: "IX_Bookings_AppUserId",
table: "Bookings",
column: "TableId");
column: "AppUserId");

migrationBuilder.CreateIndex(
name: "IX_Bookings_UserId",
name: "IX_Bookings_TableId",
table: "Bookings",
column: "UserId");
column: "TableId");

migrationBuilder.CreateIndex(
name: "IX_Ratings_AppUserId",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.ValueGeneratedOnAdd()
.HasColumnType("uuid");

b.Property<Guid>("AppUserId")
.HasColumnType("uuid");

b.Property<DateTime>("Date")
.HasColumnType("timestamp with time zone");

Expand All @@ -96,14 +99,11 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<Guid>("TableId")
.HasColumnType("uuid");

b.Property<Guid>("UserId")
.HasColumnType("uuid");

b.HasKey("Id");

b.HasIndex("TableId");
b.HasIndex("AppUserId");

b.HasIndex("UserId");
b.HasIndex("TableId");

b.ToTable("Bookings");
});
Expand Down Expand Up @@ -211,21 +211,19 @@ protected override void BuildModel(ModelBuilder modelBuilder)

modelBuilder.Entity("TableBooking.Model.Models.Booking", b =>
{
b.HasOne("TableBooking.Model.Models.Table", "Table")
b.HasOne("TableBooking.Model.Models.AppUser", null)
.WithMany("Bookings")
.HasForeignKey("TableId")
.HasForeignKey("AppUserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();

b.HasOne("TableBooking.Model.Models.AppUser", "User")
b.HasOne("TableBooking.Model.Models.Table", "Table")
.WithMany("Bookings")
.HasForeignKey("UserId")
.HasForeignKey("TableId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();

b.Navigation("Table");

b.Navigation("User");
});

modelBuilder.Entity("TableBooking.Model.Models.Rating", b =>
Expand All @@ -249,13 +247,11 @@ protected override void BuildModel(ModelBuilder modelBuilder)

modelBuilder.Entity("TableBooking.Model.Models.Table", b =>
{
b.HasOne("TableBooking.Model.Models.Restaurant", "Restaurant")
b.HasOne("TableBooking.Model.Models.Restaurant", null)
.WithMany("Tables")
.HasForeignKey("RestaurantId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();

b.Navigation("Restaurant");
});

modelBuilder.Entity("TableBooking.Model.Models.AppUser", b =>
Expand Down
2 changes: 1 addition & 1 deletion TableBookingAPI/TableBooking.Model/Models/Booking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class Booking : Entity
public DateTime Date { get; set; }
public int DurationInMinutes { get; set; }
public Table Table { get; set; }
public AppUser User { get; set; }
public Guid AppUserId { get; set; }
public Guid TableId { get; set; }
}
}
2 changes: 1 addition & 1 deletion TableBookingAPI/TableBooking.Model/Models/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class Table : Entity
{
public int NumberOfSeats { get; set; }
public Restaurant Restaurant { get; set; }
public Guid RestaurantId { get; set; }
public IEnumerable<Booking> Bookings { get; set; }
}
}
4 changes: 4 additions & 0 deletions TableBookingAPI/TableBooking.Model/TableBooking.Model.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.8" />
</ItemGroup>

<ItemGroup>
<Folder Include="Migrations\" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions TableBookingAPI/TableBooking/Controllers/BookingController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public BookingController(IBookingService bookingService, UserManager<AppUser> us


[HttpGet("GetAllUserBookings")]
[ProducesResponseType(typeof(List<BookingDto>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> GetUserBookings()
{
var userId = Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier));
Expand Down
3 changes: 2 additions & 1 deletion TableBookingAPI/TableBooking/Interfaces/ITableService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using TableBooking.Model.Dtos.TableDtos;
using TableBooking.Model.Models;

namespace TableBooking.Api.Interfaces
{
Expand All @@ -11,6 +12,6 @@ public interface ITableService
public Task<IActionResult> CreateTableAsync(TableDto dto);
public Task<IActionResult> UpdateTableAsync(TableDto dto);
public Task<IActionResult> DeleteTableAsync(Guid tableId);
public Task<Table> GetTableObjectByIdAsync(Guid tableId);
}

}
4 changes: 0 additions & 4 deletions TableBookingAPI/TableBooking/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@
options.User.RequireUniqueEmail = true;
options.ClaimsIdentity.UserIdClaimType = ClaimTypes.NameIdentifier;
});

//builder.Services.AddHttpContextAccessor();
builder.Services.AddAuthorization();

builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();

builder.Services.AddTransient<ITableConverter, TableConverter>(); // doczytaj debilu
Expand Down
16 changes: 10 additions & 6 deletions TableBookingAPI/TableBooking/Services/BookingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,28 @@ public class BookingService : IBookingService
{
public IUnitOfWork _unitOfWork;
private readonly ITableConverter _tableConverter;
private readonly ITableService _tableService;

public BookingService(IUnitOfWork unitOfWork, ITableConverter tableConverter)

public BookingService(IUnitOfWork unitOfWork, ITableConverter tableConverter, ITableService tableService)
{
_unitOfWork = unitOfWork;
_tableConverter = tableConverter;
_tableService = tableService;
}
public async Task<IActionResult> CreateBookingAsync(CreateBookingDto request, Guid userId)
{
//string userId = User.FindFirstValue(ClaimTypes.NameIdentifier); // Retrieve the authenticated user's ID

var table = await _tableService.GetTableObjectByIdAsync(request.TableId);
var newBooking = new Booking
{
Date = request.Date,
DurationInMinutes = request.DurationInMinutes,
TableId = request.TableId
TableId = request.TableId,
AppUserId = userId,
Table = table
};


await _unitOfWork.BookingRepository.InsertAsync(newBooking);
await _unitOfWork.SaveChangesAsync();

Expand Down Expand Up @@ -68,7 +72,7 @@ public async Task<IActionResult> GetBookingByIdAsync(Guid bookingId, Guid userId
Date = booking.Date,
DurationInMinutes = booking.DurationInMinutes,
TableDto = _tableConverter.TableToTableDto(booking.Table),
UserId = booking.User.Id
UserId = userId
};
return new OkObjectResult(bookingDto);
}
Expand Down
8 changes: 8 additions & 0 deletions TableBookingAPI/TableBooking/Services/TableService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public async Task<IActionResult> GetTableByIdAsync(Guid tableId)
return new BadRequestObjectResult($"Can't find table with {tableId}");
return new OkObjectResult(table);
}

public async Task<Table> GetTableObjectByIdAsync(Guid tableId)
{
var table = await _unitOfWork.TableRepository.GetByIdAsync(tableId);
if (table == null)
throw new BadHttpRequestException($"Table id: {tableId} doesn't exist.");
return table;
}

public async Task<IActionResult> GetTableByRestaurantAsync(Guid restaurantId)
{
Expand Down