Changeset 13f1472 for resTools_backend/backend/Services
- Timestamp:
- 09/18/22 18:09:53 (2 years ago)
- Branches:
- master
- Parents:
- 49b0bbd
- Location:
- resTools_backend/backend/Services
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
resTools_backend/backend/Services/MenuService.cs
r49b0bbd r13f1472 10 10 public Task AddMenu(CreateMenuItemRequest menu); 11 11 public Task RemoveMenu(int id); 12 public Task UploadImage(int id, IFormFile file); 12 13 } 13 14 public class MenuService : IMenuService … … 29 30 Title = menu.Title, 30 31 Description = menu.Description, 31 Price = menu.Price 32 }); 32 Price = menu.Price, 33 Alergens = menu.Alergens, 34 IsVipOnly = menu.IsVipOnly, 35 Image = Array.Empty<byte>() 36 }) ; 33 37 _context.Restoraunts.Update(res); 34 38 await _context.SaveChangesAsync(); … … 42 46 await _context.SaveChangesAsync(); 43 47 } 48 49 public async Task UploadImage(int id, IFormFile file) 50 { 51 using (var memoryStream = new MemoryStream()) 52 { 53 await file.CopyToAsync(memoryStream); 54 var menuItem = await _context.MenuItems.FindAsync(id); 55 menuItem.Image = memoryStream.ToArray(); 56 _context.MenuItems.Update(menuItem); 57 _context.SaveChanges(); 58 } 59 } 44 60 } 45 61 } -
resTools_backend/backend/Services/RestaurantService.cs
r49b0bbd r13f1472 45 45 Title = x.Title, 46 46 Description = x.Description, 47 Price = x.Price 47 Price = x.Price, 48 Alergens = x.Alergens, 49 Image = String.Format("data:image/png;base64,{0}", Convert.ToBase64String(x.Image)), 50 IsVipOnly = x.IsVipOnly 48 51 }).ToList(), 49 52 Reviews = x.Reviews.Select(x => new ReviewResponse() … … 55 58 Title = x.Title, 56 59 Username = x.User == null ? "Anonymous" : x.User.Email 57 }).ToList(), 58 AverageReview = x.Reviews.Count>0 ? x.Reviews.Select(x => x.Stars).Average() : 0 60 }).ToList() 59 61 }) 60 62 .FirstOrDefaultAsync(); 63 var reviews = await _context.Reviews.ToListAsync(); 64 res.AverageReview = reviews.Select(x => x.Stars).Sum(); 61 65 return res; 62 66 } -
resTools_backend/backend/Services/ReviewService.cs
r49b0bbd r13f1472 1 1 using backend.Data; 2 2 using backend.DTOs; 3 using backend.Email; 3 4 using backend.Entities; 4 5 using Microsoft.EntityFrameworkCore; … … 15 16 { 16 17 private readonly DataContext _context = null; 18 private readonly IEmailSender _emailSender; 17 19 18 public ReviewService(DataContext context )20 public ReviewService(DataContext context, IEmailSender emailSender) 19 21 { 20 22 _context = context; 23 _emailSender = emailSender; 24 21 25 } 22 26 … … 39 43 _context.Restoraunts.Update(res); 40 44 await _context.SaveChangesAsync(); 45 46 if (review.Stars < 3) 47 { 48 var adminUser = await _context.Users.FirstOrDefaultAsync(x => x.IsAdmin); 49 await _emailSender.SendEmailAsync("Добивте лоша оценка", $"<html><h1>{review.Title}</h1><p>{review.Description}</p></html>", adminUser.Email); 50 } 41 51 } 42 52 -
resTools_backend/backend/Services/UserService.cs
r49b0bbd r13f1472 20 20 Task<AuthenticateResponse> Register(CreateUserRequest req, bool isFirst); 21 21 Task<User> GetById(int id); 22 Task<User> GetByEmail(string email); 22 23 Task SendEmailConfirmation(string email); 23 24 Task SendPasswordReset(string email); 24 25 Task ConfirmEmail(User user, string checkValid); 25 26 Task ResetPassword(string checkValid, string password); 27 Task<List<UserResponse>> GetUsers(); 28 Task UpdateVipStatus(int id, bool isVip); 29 26 30 } 27 31 … … 39 43 } 40 44 45 public async Task<User> GetByEmail(string email) 46 { 47 return await _context.Users.FirstOrDefaultAsync(x => x.Email == email); 48 } 49 41 50 public async Task<AuthenticateResponse> Authenticate(AuthenticateRequest model) 42 51 { … … 49 58 var token = generateJwtToken(user); 50 59 51 return new AuthenticateResponse { Email = user.Email, Id = user.Id, Token = token, IsAdmin = user.IsAdmin, IsConfirmed = user.IsConfirmed };60 return new AuthenticateResponse { Email = user.Email, Id = user.Id, Token = token, IsAdmin = user.IsAdmin, IsConfirmed = user.IsConfirmed, isVip = user.IsVip}; 52 61 } 53 62 … … 75 84 public async Task<AuthenticateResponse> Register(CreateUserRequest req, bool isFirst) 76 85 { 77 User user = new User() { Email = req.Email, Password = req.Password, IsAdmin = isFirst, IsConfirmed = false }; 86 var exists = await _context.Users.FirstOrDefaultAsync(x => x.Email == req.Email); 87 if(exists != null && req.IsConfirmed) 88 { 89 return new AuthenticateResponse { Email = exists.Email, Id = exists.Id, IsAdmin = exists.IsAdmin, IsConfirmed = true }; 90 } 91 User user = new User() { Email = req.Email, Password = req.Password, IsAdmin = isFirst, IsConfirmed = req.IsConfirmed, IsVip = false }; 78 92 await _context.Users.AddAsync(user); 79 93 await _context.SaveChangesAsync(); 80 94 var token = generateJwtToken(user); 81 return new AuthenticateResponse { Email = user.Email, Id = user.Id, Token = token, IsAdmin = user.IsAdmin, IsConfirmed = false};95 return new AuthenticateResponse { Email = user.Email, Id = user.Id, Token = token, IsAdmin = user.IsAdmin, IsConfirmed = req.IsConfirmed, isVip = user.IsVip }; 82 96 } 83 97 … … 125 139 } 126 140 141 public async Task<List<UserResponse>> GetUsers() 142 { 143 return await _context.Users.Select(x => new UserResponse() 144 { 145 Email = x.Email, 146 Id = x.Id, 147 IsVip = x.IsVip 148 }).OrderBy(x => x.Id).ToListAsync(); 149 } 150 151 public async Task UpdateVipStatus(int id, bool isVip) 152 { 153 var user = await _context.Users.FindAsync(id); 154 user.IsVip = isVip; 155 _context.Users.Update(user); 156 await _context.SaveChangesAsync(); 157 } 158 127 159 private string generateJwtToken(User user) 128 160 {
Note:
See TracChangeset
for help on using the changeset viewer.