API возвращает ноль в свойствах отзывов - PullRequest
1 голос
/ 11 ноября 2019

Я пытаюсь создать простой метод GET для получения информации об объекте отеля, но застрял при попытке получить отзывы (которые были добавлены пользователем). В базе данных сущность Обзора хранится в 5 свойствах:

int Id(Primarykey)
int SenderId(User who sent review)
int RecipientId(The hotel reviewed by user)
int point
string comment 

Модель Обзора:

 public class Review
{
    public int Id { get; set; }
    public User Sender { get; set; }
    public int SenderId {get; set; }
    public Hotel Recipient { get; set; }
    public int RecipientId { get; set; }
    public DateTime DateAdded { get; set; }
    public int Point { get; set; }
    public string Comment { get; set; }
    public bool SenderDeleted { get; set; }
}

Мотель отеля:

 public class Hotel 
{
    public int Id { get; set; }
    public string HotelName { get; set; }
    public string Description { get; set; }
    public double Price { get; set; }
    public string Address { get; set; }
    public int Rooms { get; set; }
    public int Beds { get; set; }
    public int Bathrooms { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
    public bool Status { get; set; }
    public ICollection<Review> ReviewsReceived { get; set; }

}

в Myrepository.cs. Я объявил функцию GetHotel:

public async Task<Hotel> GetHotel(int id)
    {
        var hotel = await _context.Hotels.Include(r => 
        r.ReviewsReceived).FirstOrDefaultAsync(h => h.Id == id);
        return hotel;
    }

в HotelsController.cs:

[HttpGet("{id}")]
    public async Task<IActionResult> GetHotel(int id)
    {
        var hotel = await Myrepository.GetHotel(id);
        var hotelToReturn = IMapper.Map<HotelForDetailedDto>(hotel);
        return Ok(hotelToReturn);
    }

в HotelForDetailedDto.cs:

 public class HotelForDetailedDto
{   
    public string HotelName { get; set; }
    public string Description { get; set; }
    public double Price { get; set; }
    public string Address { get; set; }
    public int Rooms { get; set; }
    public int Beds { get; set; }
    public int Bathrooms { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
    public bool Status { get; set; }
    public ICollection<ReviewForDetailedDto> Reviews { get; set; }
}

в ReviewForDetailDto.cs:

public class ReviewForDetailedDto
{
    public int Id { get; set; }
    public int SenderId { get; set; }  // userId
    public int Point { get; set; }
    public string Comment { get; set; }
}

Я создал метод Map для проверки:

CreateMap<Review, ReviewForDetailedDto>();

И вот что я получил:

{
"hotelName": "Dentrex",
"description": "Eu incididunt pariatur non veniam veniam reprehenderit 
cillum adipisicing consequat cillum aliqua in. Ut ut reprehenderit commodo 
tempor cillum in sit anim labore in et proident commodo. Fugiat eni.\r\n",
"price": 2349.79,
"address": "Gelston Avenue",
"rooms": 487,
"beds": 2,
"bathrooms": 2,
"city": "Ronco",
"country": "Algeria",
"status": false,
"reviews": null
}

Так что я хочу, чтобы отзывы возвращали всесвойство, которое было объявлено в ReviewForDetailDto через AutoMapper.

Пожалуйста, помогите.

1 Ответ

3 голосов
/ 11 ноября 2019

ваше " Отзывы" Полученное"имя свойства в вашей отеле модель не совпадает с названием" Отзывы"в вашем HotelForDetailedDto модель

вы можете переименовать его или настроить отображение, используя метод forMember ()

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...