Changeset 899b19d for resTools_backend/backend
- Timestamp:
- 07/10/22 10:27:45 (2 years ago)
- Branches:
- master
- Children:
- a26f6a1
- Parents:
- cc4db18
- Location:
- resTools_backend/backend
- Files:
-
- 7 added
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
resTools_backend/backend/Controllers/RestaurantsController.cs
rcc4db18 r899b19d 20 20 } 21 21 22 /* 22 23 [Authorize] 23 24 [HttpPost()] … … 30 31 return Ok(); 31 32 } 33 */ 32 34 33 35 [HttpGet()] -
resTools_backend/backend/Controllers/UsersController.cs
rcc4db18 r899b19d 13 13 { 14 14 private readonly IUserService _userService = null; 15 private readonly IRestaurantService _restaurantService = null; 15 16 16 public UsersController(IUserService userService )17 public UsersController(IUserService userService, IRestaurantService restaurantService) 17 18 { 18 19 _userService = userService; 20 _restaurantService = restaurantService; 19 21 } 20 22 … … 45 47 public async Task<AuthenticateResponse> Register(CreateUserRequest req) 46 48 { 47 var response = await _userService.Register(req); 49 bool isFirst = await _restaurantService.GetRestaurant() == null; 50 var response = await _userService.Register(req, isFirst); 51 if (isFirst) 52 { 53 await _restaurantService.CreateRestaurant("", response.Id); 54 } 48 55 return response; 49 56 } -
resTools_backend/backend/DTOs/AuthenticateResponse.cs
rcc4db18 r899b19d 13 13 [JsonProperty] 14 14 public string Token { get; set; } 15 [JsonProperty] 16 public bool IsAdmin { get; set; } 15 17 } -
resTools_backend/backend/DTOs/RestaurantResponse.cs
rcc4db18 r899b19d 14 14 public List<MenuItemResponse> Menu { get; set; } 15 15 [JsonProperty] 16 public List<ReviewResponse> Reviews { get; set; } 17 [JsonProperty] 18 public double AverageReview { get; set; } 19 [JsonProperty] 16 20 public string Base64Image { get; set; } 17 21 } -
resTools_backend/backend/Data/DataContext.cs
rcc4db18 r899b19d 66 66 } 67 67 68 private DbSet<Review> reviews; 69 public DbSet<Review> Reviews 70 { 71 get 72 { 73 if (reviews == null) 74 { 75 reviews = Set<Review>(); 76 } 77 78 return reviews; 79 } 80 } 81 68 82 69 83 protected override void OnModelCreating(ModelBuilder modelBuilder) … … 89 103 .WithOne(b => b.Restaurant); 90 104 modelBuilder.Entity<Restaurant>() 91 .HasMany(p => p. Menu)105 .HasMany(p => p.Reviews) 92 106 .WithOne(b => b.Restaurant); 93 94 107 95 108 // … … 108 121 .HasOne(p => p.Restaurant) 109 122 .WithMany(b => b.Menu); 123 124 // 125 // Review 126 // 127 modelBuilder.Entity<Review>().Property(x => x.Id).IsRequired().ValueGeneratedOnAdd(); 128 modelBuilder.Entity<Review>() 129 .HasOne(p => p.Restaurant) 130 .WithMany(b => b.Reviews); 131 modelBuilder.Entity<Review>() 132 .HasOne(p => p.User); 110 133 } 111 134 } -
resTools_backend/backend/Entities/Restaurant.cs
rcc4db18 r899b19d 10 10 public virtual ICollection<Reservation> Reservations { get; set; } 11 11 public virtual ICollection<MenuItem> Menu { get; set; } 12 public virtual ICollection<Review> Reviews { get; set; } 12 13 public string Name { get; set; } 13 14 public string Address { get; set; } -
resTools_backend/backend/Entities/User.cs
rcc4db18 r899b19d 8 8 public string Email { get; set; } 9 9 public string Password { get; set; } 10 [JsonIgnore]10 public bool IsAdmin { get; set; } 11 11 public virtual Restaurant Restaurant { get; set; } 12 12 } -
resTools_backend/backend/Migrations/DataContextModelSnapshot.cs
rcc4db18 r899b19d 131 131 }); 132 132 133 modelBuilder.Entity("backend.Entities.Review", b => 134 { 135 b.Property<int>("Id") 136 .ValueGeneratedOnAdd() 137 .HasColumnType("integer"); 138 139 NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id")); 140 141 b.Property<DateTime>("CreatedAt") 142 .HasColumnType("timestamp with time zone"); 143 144 b.Property<string>("Description") 145 .IsRequired() 146 .HasColumnType("text"); 147 148 b.Property<int>("RestaurantId") 149 .HasColumnType("integer"); 150 151 b.Property<int>("Stars") 152 .HasColumnType("integer"); 153 154 b.Property<string>("Title") 155 .IsRequired() 156 .HasColumnType("text"); 157 158 b.Property<int>("UserId") 159 .HasColumnType("integer"); 160 161 b.HasKey("Id"); 162 163 b.HasIndex("RestaurantId"); 164 165 b.HasIndex("UserId"); 166 167 b.ToTable("Reviews"); 168 }); 169 133 170 modelBuilder.Entity("backend.Entities.User", b => 134 171 { … … 142 179 .IsRequired() 143 180 .HasColumnType("text"); 181 182 b.Property<bool>("IsAdmin") 183 .HasColumnType("boolean"); 144 184 145 185 b.Property<string>("Password") … … 183 223 }); 184 224 225 modelBuilder.Entity("backend.Entities.Review", b => 226 { 227 b.HasOne("backend.Entities.Restaurant", "Restaurant") 228 .WithMany("Reviews") 229 .HasForeignKey("RestaurantId") 230 .OnDelete(DeleteBehavior.Cascade) 231 .IsRequired(); 232 233 b.HasOne("backend.Entities.User", "User") 234 .WithMany() 235 .HasForeignKey("UserId") 236 .OnDelete(DeleteBehavior.Cascade) 237 .IsRequired(); 238 239 b.Navigation("Restaurant"); 240 241 b.Navigation("User"); 242 }); 243 185 244 modelBuilder.Entity("backend.Entities.Restaurant", b => 186 245 { … … 188 247 189 248 b.Navigation("Reservations"); 249 250 b.Navigation("Reviews"); 190 251 }); 191 252 -
resTools_backend/backend/Program.cs
rcc4db18 r899b19d 47 47 builder.Services.AddScoped<IReservationService, ReservationService>(); 48 48 builder.Services.AddScoped<IMenuService, MenuService>(); 49 builder.Services.AddScoped<IReviewService, ReviewService>(); 49 50 builder.Services.AddScoped<ISmsService, SmsService>(); 50 51 -
resTools_backend/backend/Services/RestaurantService.cs
rcc4db18 r899b19d 32 32 { 33 33 RestaurantResponse res = await _context.Restoraunts 34 .Include(x => x.Menu) 35 .Include(x => x.Reviews).ThenInclude(x => x.User) 34 36 .Select(x => new RestaurantResponse() 35 37 { … … 44 46 Description = x.Description, 45 47 Price = x.Price 46 }).ToList() 48 }).ToList(), 49 Reviews = x.Reviews.OrderByDescending(x => x.CreatedAt).Select(x => new ReviewResponse() 50 { 51 Id = x.Id, 52 Title = x.Title, 53 Description = x.Description, 54 Stars = x.Stars, 55 CreatedAt = x.CreatedAt, 56 Username = x.User == null ? "Anonymous" : x.User.Email 57 }).ToList(), 58 AverageReview = x.Reviews.Count>0 ? x.Reviews.Select(x => x.Stars).Average() : 0 47 59 }) 48 60 .FirstOrDefaultAsync(); -
resTools_backend/backend/Services/UserService.cs
rcc4db18 r899b19d 15 15 { 16 16 Task<AuthenticateResponse> Authenticate(AuthenticateRequest model); 17 Task<AuthenticateResponse> Register(CreateUserRequest req );17 Task<AuthenticateResponse> Register(CreateUserRequest req, bool isFirst); 18 18 Task<User> GetById(int id); 19 19 } … … 40 40 var token = generateJwtToken(user); 41 41 42 return new AuthenticateResponse { Email = user.Email, Id = user.Id, Token = token };42 return new AuthenticateResponse { Email = user.Email, Id = user.Id, Token = token, IsAdmin = user.IsAdmin}; 43 43 } 44 44 … … 48 48 } 49 49 50 public async Task<AuthenticateResponse> Register(CreateUserRequest req )50 public async Task<AuthenticateResponse> Register(CreateUserRequest req, bool isFirst) 51 51 { 52 User user = new User() { Email = req.Email, Password = req.Password };52 User user = new User() { Email = req.Email, Password = req.Password, IsAdmin = isFirst }; 53 53 await _context.Users.AddAsync(user); 54 54 await _context.SaveChangesAsync(); 55 55 var token = generateJwtToken(user); 56 return new AuthenticateResponse { Email = user.Email, Id = user.Id, Token = token };56 return new AuthenticateResponse { Email = user.Email, Id = user.Id, Token = token, IsAdmin = user.IsAdmin }; 57 57 } 58 58
Note:
See TracChangeset
for help on using the changeset viewer.