#3 Добавил сущность отзыва о телефоне.

parent fe443008
......@@ -142,11 +142,32 @@ namespace PhoneStore.Controllers
if (!phoneId.HasValue) return RedirectToAction("Error", "Errors", new {statusCode = 777});
var phone = _db.Phones
.Include(p => p.Brand)
.Include(p => p.Feedbacks)
.ThenInclude(f => f.User)
.FirstOrDefault(p => p.Id == phoneId);
if (phone is null)
return RedirectToAction("Error", "Errors", new {statusCode = 777});
return View(phone);
var phoneViewModel = new PhoneViewModel
{
Brand = phone.Brand,
Feedbacks = phone.Feedbacks.Select(f => new FeedbackViewModel
{
Id = f.Id,
Phone = f.Phone,
Text = f.Text,
User = f.User,
CreationDateTime = f.CreationDateTime,
UserId = f.UserId,
PhoneId = f.PhoneId
})
.OrderByDescending(f => f.CreationDateTime)
.ToList(),
Image = phone.Image,
Name = phone.Name,
Price = phone.Price,
Id = phone.Id
};
return View(phoneViewModel);
}
}
}
\ No newline at end of file
using System;
using Microsoft.EntityFrameworkCore.Migrations;
namespace PhoneStore.Migrations
{
public partial class AddCreationDateTimeField : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<DateTime>(
name: "CreationDateTime",
table: "Feedbacks",
type: "timestamp without time zone",
nullable: false,
defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "CreationDateTime",
table: "Feedbacks");
}
}
}
......@@ -159,6 +159,9 @@ namespace PhoneStore.Migrations
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<DateTime>("CreationDateTime")
.HasColumnType("timestamp without time zone");
b.Property<int>("PhoneId")
.HasColumnType("integer");
......
using System;
namespace PhoneStore.Models
{
public class Feedback
......@@ -6,6 +8,7 @@ namespace PhoneStore.Models
public int UserId { get; set; }
public int PhoneId { get; set; }
public string Text { get; set; }
public DateTime CreationDateTime { get; set; }
public User User { get; set; }
public Phone Phone { get; set; }
}
......
using System.Collections.Generic;
namespace PhoneStore.Models
{
public class Phone
......@@ -8,5 +10,6 @@ namespace PhoneStore.Models
public int? BrandId { get; set; }
public Brand Brand { get; set; }
public string Image { get; set; }
public List<Feedback> Feedbacks { get; set; }
}
}
\ No newline at end of file
......@@ -50,6 +50,5 @@ namespace PhoneStore.Services
_db.Phones.Add(entity.MapToPhone(imagePath));
await _db.SaveChangesAsync();
}
}
}
\ No newline at end of file
namespace PhoneStore.ViewModels
{
public class FeedbackCreateViewModel
{
public int UserId { get; set; }
public int PhoneId { get; set; }
public string Text { get; set; }
}
}
\ No newline at end of file
using System;
using PhoneStore.Models;
namespace PhoneStore.ViewModels
{
public class FeedbackViewModel
{
public int Id { get; set; }
public int UserId { get; set; }
public int PhoneId { get; set; }
public string Text { get; set; }
public DateTime CreationDateTime { get; set; }
public User User { get; set; }
public Phone Phone { get; set; }
}
}
\ No newline at end of file
using System.Collections.Generic;
using PhoneStore.Models;
namespace PhoneStore.ViewModels
{
public class PhoneViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public Brand Brand { get; set; }
public string Image { get; set; }
public List<FeedbackViewModel> Feedbacks { get; set; }
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment