Skip to content

Commit 80d5ea5

Browse files
author
Paul Frank
committed
bookings crud WORKING
1 parent b9db278 commit 80d5ea5

File tree

13 files changed

+369
-28
lines changed

13 files changed

+369
-28
lines changed

TableBookingAPI/TableBooking.Logic/Repositories/BookingRepository.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ public BookingRepository(TableBookingContext context) : base(context)
1313

1414
public async Task<IEnumerable<Booking>> GetAllBookingsForSpecificUserAsync(Guid userId)
1515
{
16-
return await _objectSet.Where(x => x.Id.Equals(userId)).ToListAsync();
16+
return await _objectSet.Where(x => x.AppUserId.Equals(userId)).ToListAsync();
1717
}
1818

1919
public async Task<Booking?> GetBookingByIdForSpecificUserAsync(Guid bookingId, Guid userId)
2020
{
21-
return await _objectSet.FirstOrDefaultAsync(x => x.Id.Equals(bookingId) && x.Id.Equals(userId));
21+
return await _objectSet.FirstOrDefaultAsync(x => x.Id.Equals(bookingId) && x.AppUserId.Equals(userId));
2222
}
2323
}
2424
}

TableBookingAPI/TableBooking.Logic/Repositories/GenericRepository.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,23 @@ public async Task Delete(object id)
3535
_objectSet.Remove(objectToDelete);
3636
}
3737

38-
public async virtual Task Update(T entity)
38+
public async Task Update(T entity)
3939
{
40-
await Update(entity);
41-
}
40+
var existingEntity = await _objectSet.FindAsync(GetKeyValues(entity));
41+
42+
if (existingEntity != null)
43+
{
44+
_context.Entry(existingEntity).State = EntityState.Detached;
45+
}
4246

47+
_objectSet.Update(entity);
48+
await _context.SaveChangesAsync();
49+
}
50+
51+
private object[] GetKeyValues(T entity)
52+
{
53+
var keyProperties = _context.Model.FindEntityType(typeof(T)).FindPrimaryKey().Properties;
54+
return keyProperties.Select(prop => prop.PropertyInfo.GetValue(entity)).ToArray();
55+
}
4356
}
4457
}

TableBookingAPI/TableBooking.Model/Dtos/BookingDtos/BookingDto.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class BookingDto
77
public Guid Id { get; set; }
88
public DateTime Date { get; set; }
99
public int DurationInMinutes { get; set; }
10-
public TableDto TableDto { get; set; }
11-
public Guid UserId { get; set; }
10+
public int AmountOfPeople { get; set; }
11+
public Guid AppUserId { get; set; }
1212
}
1313
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
namespace TableBooking.Model.Dtos.BookingDtos
1+
using TableBooking.Model.Models;
2+
3+
namespace TableBooking.Model.Dtos.BookingDtos
24
{
35
public class CreateBookingDto
46
{
57
public DateTime Date { get; set; }
68
public int DurationInMinutes { get; set; }
9+
public int AmountOfPeople { get; set; }
710
public Guid TableId { get; set; }
811
}
912
}

TableBookingAPI/TableBooking.Model/Dtos/BookingDtos/UpdateBookingDto.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ public class UpdateBookingDto
44
{
55
public DateTime Date { get; set; }
66
public int DurationInMinutes { get; set; }
7-
public Guid TableId { get; set; }
7+
public int AmountOfPeople { get; set; }
88
}
99
}

TableBookingAPI/TableBooking.Model/Migrations/20231119221110_AmountOfPeopleInBookings.Designer.cs

Lines changed: 279 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
#nullable disable
4+
5+
namespace TableBooking.Model.Migrations
6+
{
7+
public partial class AmountOfPeopleInBookings : Migration
8+
{
9+
protected override void Up(MigrationBuilder migrationBuilder)
10+
{
11+
migrationBuilder.AddColumn<int>(
12+
name: "AmountOfPeople",
13+
table: "Bookings",
14+
type: "integer",
15+
nullable: false,
16+
defaultValue: 0);
17+
}
18+
19+
protected override void Down(MigrationBuilder migrationBuilder)
20+
{
21+
migrationBuilder.DropColumn(
22+
name: "AmountOfPeople",
23+
table: "Bookings");
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)