97 lines
3.1 KiB
C#

using AutoMapper;
using FichaBackend.Models;
using FichaBackend.Models.DTO;
using Microsoft.EntityFrameworkCore;
namespace FichaBackend.Services;
public interface IAdminService
{
public Task AddMuseum(MuseumDto museumDto);
public Task<string> AddGuidePerson(GuideDto guideDto);
public Task<string> AddHotel(HotelDto hotelDto);
public Task<string> AddAttraction(AttractionDto attractionDto);
}
public class AdminService : IAdminService
{
private readonly DatabaseContext _databaseContext;
private readonly IMapper _mapper;
public AdminService(DatabaseContext databaseContext, IMapper mapper)
{
_databaseContext = databaseContext;
_mapper = mapper;
}
public async Task AddMuseum(MuseumDto museumDto)
{
City c = await _databaseContext.Cities.FirstOrDefaultAsync(x => x.Name.ToLower() == museumDto.City.ToLower());
if (c==null)
return;
Museum m = new();
m.Name = museumDto.Name;
m.City = c;
m.Price = museumDto.Price;
m.Latitude = museumDto.Latitude;
m.Longtitude = museumDto.Longtitude;
m.Address = museumDto.Address;
await _databaseContext.Museums.AddAsync(m);
await _databaseContext.SaveChangesAsync();
}
public async Task<string> AddGuidePerson(GuideDto guideDto)
{
Guide g = new();
g.City = new List<City>();
foreach (var city in guideDto.City)
{
var c = await _databaseContext.Cities.FirstOrDefaultAsync(x => x.Name.ToLower() == city.ToLower());
if (c==null)
return $"City {city} not exist";
g.City.Append(c);
}
g.Rating = guideDto.Rating;
g.PhoneNumber = guideDto.PhoneNumber;
g.ContactUrl = guideDto.ContactUrl;
g.FullName = guideDto.FullName;
await _databaseContext.Guides.AddAsync(g);
await _databaseContext.SaveChangesAsync();
return "OK";
}
public async Task<string> AddHotel(HotelDto hotelDto)
{
var c = await _databaseContext.Cities.FirstOrDefaultAsync(x => x.Name.ToLower() == hotelDto.City.ToLower());
if (c==null)
return $"City {c} not exist";
Hotel h = new();
h.Name = hotelDto.Name;
h.City = hotelDto.City;
h.Price = hotelDto.Price;
h.Address = hotelDto.Address;
h.ImageURL = hotelDto.ImageURL;
await _databaseContext.Hotels.AddAsync(h);
await _databaseContext.SaveChangesAsync();
return "OK";
}
public async Task<string> AddAttraction(AttractionDto attractionDto)
{
var c = await _databaseContext.Cities.FirstOrDefaultAsync(x => x.Name.ToLower() == attractionDto.City.ToLower());
if (c==null)
return $"City {c} not exist";
Attraction h = new();
h.Name = attractionDto.Name;
h.City = attractionDto.City;
h.Address = attractionDto.Address;
h.ImageURL = attractionDto.ImageURL;
await _databaseContext.Attractions.AddAsync(h);
await _databaseContext.SaveChangesAsync();
return "OK";
}
}