Changeset cc4db18 for resTools_backend
- Timestamp:
- 07/06/22 13:13:35 (2 years ago)
- Branches:
- master
- Children:
- 899b19d
- Parents:
- d76b7ee
- Location:
- resTools_backend/backend
- Files:
-
- 12 added
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
resTools_backend/backend/Controllers/ReservationsController.cs
rd76b7ee rcc4db18 27 27 } 28 28 29 [Authorize] 30 [HttpGet("new")] 31 public async Task<List<ReservationResponse>> GetReservationsNew() 32 { 33 return await _reservationService.GetNewReservations(); 34 } 35 29 36 [HttpPost()] 30 37 public async Task<IActionResult> CreateReservation([FromBody] CreateReservationRequest req) … … 35 42 36 43 [Authorize] 37 [HttpPut("{rid} ")]44 [HttpPut("{rid}/status")] 38 45 public async Task<IActionResult> ChangeStatus(int rid, [FromQuery] ReservationStatus status) 39 46 { … … 41 48 return Ok(); 42 49 } 50 51 [Authorize] 52 [HttpPut("{rid}/table")] 53 public async Task<IActionResult> ChangeTable(int rid, [FromQuery] int tableId) 54 { 55 await _reservationService.AssignTable(tableId, rid); 56 return Ok(); 57 } 43 58 } -
resTools_backend/backend/Controllers/RestaurantsController.cs
rd76b7ee rcc4db18 37 37 return response; 38 38 } 39 40 [HttpPost("upload")] 41 public async Task<IActionResult> UploadImage([FromForm] IFormFile file) 42 { 43 await _restaurantService.UploadImage(file); 44 return Ok(); 45 } 46 47 [Authorize] 48 [HttpPut()] 49 public async Task<IActionResult> UpdateRestaurant([FromBody] UpdateRestaurantRequest req) 50 { 51 await _restaurantService.UpdateRestaurant(req); 52 return Ok(); 53 } 39 54 } -
resTools_backend/backend/DTOs/ReservationResponse.cs
rd76b7ee rcc4db18 22 22 [JsonProperty] 23 23 public string ContactNumber { get; set; } 24 [JsonProperty] 25 public int Table { get; set; } 24 26 } 25 27 } -
resTools_backend/backend/DTOs/RestaurantResponse.cs
rd76b7ee rcc4db18 6 6 { 7 7 [JsonProperty] 8 public int Id{ get; set; }8 public string Name { get; set; } 9 9 [JsonProperty] 10 public int? OwnerId{ get; set; }10 public string Address { get; set; } 11 11 [JsonProperty] 12 public string Name { get; set; } 12 public string Phone { get; set; } 13 [JsonProperty] 14 public List<MenuItemResponse> Menu { get; set; } 15 [JsonProperty] 16 public string Base64Image { get; set; } 13 17 } 14 18 } -
resTools_backend/backend/Data/DataContext.cs
rd76b7ee rcc4db18 52 52 } 53 53 54 private DbSet<MenuItem> menuItems; 55 public DbSet<MenuItem> MenuItems 56 { 57 get 58 { 59 if (menuItems == null) 60 { 61 menuItems = Set<MenuItem>(); 62 } 63 64 return menuItems; 65 } 66 } 67 54 68 55 69 protected override void OnModelCreating(ModelBuilder modelBuilder) … … 74 88 .HasMany(p => p.Reservations) 75 89 .WithOne(b => b.Restaurant); 90 modelBuilder.Entity<Restaurant>() 91 .HasMany(p => p.Menu) 92 .WithOne(b => b.Restaurant); 93 76 94 77 95 // … … 82 100 .HasOne(p => p.Restaurant) 83 101 .WithMany(b => b.Reservations); 102 103 // 104 // MenuItem 105 // 106 modelBuilder.Entity<MenuItem>().Property(x => x.Id).IsRequired().ValueGeneratedOnAdd(); 107 modelBuilder.Entity<MenuItem>() 108 .HasOne(p => p.Restaurant) 109 .WithMany(b => b.Menu); 84 110 } 85 111 } -
resTools_backend/backend/Entities/Reservation.cs
rd76b7ee rcc4db18 14 14 public string ContactName { get; set; } 15 15 public string ContactNumber { get; set; } 16 public int Table { get; set; } 16 17 } 17 18 -
resTools_backend/backend/Entities/Restaurant.cs
rd76b7ee rcc4db18 9 9 public virtual User Owner { get; set; } 10 10 public virtual ICollection<Reservation> Reservations { get; set; } 11 public virtual ICollection<MenuItem> Menu { get; set; } 11 12 public string Name { get; set; } 13 public string Address { get; set; } 14 public string Phone { get; set; } 15 public byte[] Image { get; set; } 12 16 } 13 17 } -
resTools_backend/backend/Migrations/DataContextModelSnapshot.cs
rd76b7ee rcc4db18 23 23 NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); 24 24 25 modelBuilder.Entity("backend.Entities.MenuItem", b => 26 { 27 b.Property<int>("Id") 28 .ValueGeneratedOnAdd() 29 .HasColumnType("integer"); 30 31 NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id")); 32 33 b.Property<string>("Description") 34 .IsRequired() 35 .HasColumnType("text"); 36 37 b.Property<int>("Price") 38 .HasColumnType("integer"); 39 40 b.Property<int>("RestaurantId") 41 .HasColumnType("integer"); 42 43 b.Property<string>("Title") 44 .IsRequired() 45 .HasColumnType("text"); 46 47 b.HasKey("Id"); 48 49 b.HasIndex("RestaurantId"); 50 51 b.ToTable("MenuItems"); 52 }); 53 25 54 modelBuilder.Entity("backend.Entities.Reservation", b => 26 55 { … … 57 86 .HasColumnType("timestamp with time zone"); 58 87 88 b.Property<int>("Table") 89 .HasColumnType("integer"); 90 59 91 b.HasKey("Id"); 60 92 … … 72 104 NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id")); 73 105 106 b.Property<string>("Address") 107 .IsRequired() 108 .HasColumnType("text"); 109 110 b.Property<byte[]>("Image") 111 .IsRequired() 112 .HasColumnType("bytea"); 113 74 114 b.Property<string>("Name") 75 115 .IsRequired() … … 78 118 b.Property<int?>("OwnerFk") 79 119 .HasColumnType("integer"); 120 121 b.Property<string>("Phone") 122 .IsRequired() 123 .HasColumnType("text"); 80 124 81 125 b.HasKey("Id"); … … 106 150 107 151 b.ToTable("Users"); 152 }); 153 154 modelBuilder.Entity("backend.Entities.MenuItem", b => 155 { 156 b.HasOne("backend.Entities.Restaurant", "Restaurant") 157 .WithMany("Menu") 158 .HasForeignKey("RestaurantId") 159 .OnDelete(DeleteBehavior.Cascade) 160 .IsRequired(); 161 162 b.Navigation("Restaurant"); 108 163 }); 109 164 … … 130 185 modelBuilder.Entity("backend.Entities.Restaurant", b => 131 186 { 187 b.Navigation("Menu"); 188 132 189 b.Navigation("Reservations"); 133 190 }); -
resTools_backend/backend/Program.cs
rd76b7ee rcc4db18 46 46 builder.Services.AddScoped<IRestaurantService, RestaurantService>(); 47 47 builder.Services.AddScoped<IReservationService, ReservationService>(); 48 builder.Services.AddScoped<IMenuService, MenuService>(); 48 49 builder.Services.AddScoped<ISmsService, SmsService>(); 49 50 -
resTools_backend/backend/Services/ReservationService.cs
rd76b7ee rcc4db18 11 11 public Task ChangeReservationStatus(int resId, ReservationStatus status); 12 12 public Task<List<ReservationResponse>> GetReservatins(DateTime from, DateTime to); 13 public Task<List<ReservationResponse>> GetNewReservations(); 14 public Task AssignTable(int tableId, int reservationId); 13 15 } 14 16 public class ReservationService : IReservationService … … 21 23 _context = context; 22 24 _smsService = smsService; 25 } 26 27 public async Task AssignTable(int tableId, int reservationId) 28 { 29 var reservation = await _context.Reservations.FindAsync(reservationId); 30 reservation.Table = tableId; 31 _context.Update(reservation); 32 await _context.SaveChangesAsync(); 23 33 } 24 34 … … 50 60 } 51 61 62 public async Task<List<ReservationResponse>> GetNewReservations() 63 { 64 Restaurant res = await _context.Restoraunts 65 .Include(x => x.Reservations 66 .Where(x => x.ReservationStatus == ReservationStatus.New)) 67 .FirstOrDefaultAsync(); 68 var reservations = res.Reservations.Select(t => new ReservationResponse() 69 { 70 ContactName = t.ContactName, 71 ContactNumber = t.ContactNumber, 72 Persons = t.Persons, 73 StartDate = t.StartDate, 74 ReservationStatus = t.ReservationStatus, 75 Table = t.Table, 76 Id = t.Id, 77 ReservationPlace = t.ReservationPlace, 78 ReservationType = t.ReservationType 79 }).OrderByDescending(x => x.ReservationStatus == ReservationStatus.New).ToList(); 80 return reservations; 81 } 82 52 83 public async Task<List<ReservationResponse>> GetReservatins(DateTime from, DateTime to) 53 84 { … … 63 94 StartDate = t.StartDate, 64 95 ReservationStatus = t.ReservationStatus, 96 Table = t.Table, 65 97 Id = t.Id, 66 98 ReservationPlace = t.ReservationPlace, -
resTools_backend/backend/Services/RestaurantService.cs
rd76b7ee rcc4db18 10 10 public Task CreateRestaurant(string name, int userId); 11 11 public Task<RestaurantResponse> GetRestaurant(); 12 public Task UploadImage(IFormFile file); 13 public Task UpdateRestaurant(UpdateRestaurantRequest req); 12 14 } 13 15 public class RestaurantService : IRestaurantService … … 32 34 .Select(x => new RestaurantResponse() 33 35 { 34 Id = x.Id,35 36 Name = x.Name, 36 OwnerId = x.OwnerFk, 37 Address = x.Address, 38 Phone = x.Phone, 39 Base64Image = String.Format("data:image/png;base64,{0}", Convert.ToBase64String(x.Image)), 40 Menu = x.Menu.Select(x => new MenuItemResponse() 41 { 42 Id = x.Id, 43 Title = x.Title, 44 Description = x.Description, 45 Price = x.Price 46 }).ToList() 37 47 }) 38 48 .FirstOrDefaultAsync(); 39 49 return res; 40 50 } 51 52 public async Task UpdateRestaurant(UpdateRestaurantRequest req) 53 { 54 var restaurant = await _context.Restoraunts.FirstOrDefaultAsync(); 55 restaurant.Name = req.Name; 56 restaurant.Address = req.Address; 57 restaurant.Phone = req.Phone; 58 _context.Restoraunts.Update(restaurant); 59 await _context.SaveChangesAsync(); 60 } 61 62 public async Task UploadImage(IFormFile file) 63 { 64 using (var memoryStream = new MemoryStream()) 65 { 66 await file.CopyToAsync(memoryStream); 67 var restaurant = await _context.Restoraunts.FirstOrDefaultAsync(); 68 restaurant.Image = memoryStream.ToArray(); 69 _context.Restoraunts.Update(restaurant); 70 _context.SaveChanges(); 71 } 72 } 41 73 } 42 74 }
Note:
See TracChangeset
for help on using the changeset viewer.