using Microsoft.EntityFrameworkCore; using Stylist.Data.Dtos.Salon; using Stylist.Data.Models; using Stylist.Data.Models.DTOs.Salon; using Stylist.Data.Models.Entities; using Stylist.Domain.Mapping; using Stylist.Domain.Models; using Stylist.Domain.Repositories.Interfaces; namespace Stylist.Domain.Repositories.Implementations { public class SalonRepository : ISalonRepository { public SalonRepository(StylistContext context) { _context = context; } private readonly StylistContext _context; public async Task<bool> CreateAsync(Salon salon) { if (salon == null) return false; var salonToAdd = salon; _context.Salons.Add(salonToAdd); await _context.SaveChangesAsync(); return true; } public async Task<bool> UpdateAsync(int id, Salon salon) { var salonToUpdate = await _context .Salons .SingleOrDefaultAsync(s => s.Id == id); if (salonToUpdate is null) return false; salonToUpdate.Name = salon.Name; salonToUpdate.Description = salon.Description; salonToUpdate.Adress = salon.Adress; salonToUpdate.PhoneNumber = salon.PhoneNumber; await _context.SaveChangesAsync(); return true; } public async Task<IEnumerable<SalonRecommendedDto>> GetAllRecommendedAsync() { var salons = await _context .Salons .AsNoTracking() .Include(s => s.Reviews) .Include(s => s.WorkingHours) .Select(SalonMapping.MapToRecommendedDto) .OrderBy(s => s.Rating) .Take(5) .ToListAsync(); return salons; } public async Task<IEnumerable<SalonLandingDto>> GetAllAsync() { var salons = await _context .Salons .AsNoTracking() .Include(s => s.Reviews) .Include(s => s.WorkingHours) .Select(SalonMapping.MapToLandingDto) .ToListAsync(); return salons; } public async Task<ResponseResult<SalonPageDto>> GetByIdAsync(int id) { var salon = await _context .Salons .AsNoTracking() .Include(s => s.WorkingHours) .Include(s => s.Reviews) .ThenInclude(r => r.Reservation) .Select(SalonMapping.MapToSalonPageDto) .SingleOrDefaultAsync(s => s.Id == id); if (salon is null) return ResponseResult<SalonPageDto>.NotFound(nameof(salon)); return salon; } public async Task<bool> DeleteAsync(int id) { var salonToDelete = await _context .Salons .FindAsync(id); if (salonToDelete is null) return false; _context.Remove(salonToDelete); await _context.SaveChangesAsync(); return true; } } }