- Вынес создание отзыва в сервис.

parent a51fd677
using System;
using System.Linq;
using System.Text.Json;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PhoneStore.Models; using PhoneStore.Models;
using PhoneStore.ViewModels; using PhoneStore.Services.Interfaces;
using PhoneStore.ViewModels.Feedback; using PhoneStore.ViewModels.Feedback;
namespace PhoneStore.Controllers namespace PhoneStore.Controllers
{ {
public class FeedbackController : Controller public class FeedbackController : Controller
{ {
private readonly MobileContext _db;
private readonly UserManager<User> _userManager; private readonly UserManager<User> _userManager;
private readonly IFeedbackService _feedbackService;
public FeedbackController(MobileContext db, UserManager<User> userManager) public FeedbackController(
UserManager<User> userManager,
IFeedbackService feedbackService)
{ {
_db = db;
_userManager = userManager; _userManager = userManager;
_feedbackService = feedbackService;
} }
// GET [HttpGet]
public ActionResult<FeedbackViewModel> Create(FeedbackCreateViewModel model) public ActionResult<FeedbackViewModel> Create(FeedbackCreateViewModel model)
{ {
Feedback feedback = new Feedback //TODO: валидация.
{ var userId = int.Parse(_userManager.GetUserId(User));
CreationDateTime = DateTime.Now, var feedbackViewModel = _feedbackService.Create(model, userId);
Text = model.Text,
PhoneId = model.PhoneId,
UserId = int.Parse(_userManager.GetUserId(User))
};
_db.Feedbacks.Add(feedback);
_db.SaveChanges();
Feedback newFeedback = _db.Feedbacks
.Include(f => f.User)
.Include(f => f.Phone)
.First(f => f.Id == feedback.Id);
FeedbackViewModel feedbackViewModel = new FeedbackViewModel
{
Id = newFeedback.Id,
Text = newFeedback.Text,
PhoneId = newFeedback.PhoneId,
UserId = newFeedback.UserId,
CreationDateTime = newFeedback.CreationDateTime
};
return Json(feedbackViewModel); return Json(feedbackViewModel);
} }
} }
......
using PhoneStore.Models;
using PhoneStore.ViewModels.Feedback;
namespace PhoneStore.Helpers
{
public static class FeedbackMapper
{
public static FeedbackViewModel MapToFeedbackViewModel(this Feedback self)
{
FeedbackViewModel feedbackViewModel = new FeedbackViewModel
{
Id = self.Id,
Text = self.Text,
PhoneId = self.PhoneId,
UserId = self.UserId,
CreationDateTime = self.CreationDateTime
};
return feedbackViewModel;
}
}
}
\ No newline at end of file
using System;
using System.Linq;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using PhoneStore.Helpers;
using PhoneStore.Models;
using PhoneStore.Services.Interfaces;
using PhoneStore.ViewModels.Feedback;
namespace PhoneStore.Services
{
public class FeedbackService : IFeedbackService
{
private readonly MobileContext _db;
private readonly UserManager<User> _userManager;
public FeedbackService(
MobileContext db,
UserManager<User> userManager)
{
_db = db;
_userManager = userManager;
}
public FeedbackViewModel Create(FeedbackCreateViewModel model, int userId)
{
Feedback feedback = new Feedback
{
CreationDateTime = DateTime.Now,
Text = model.Text,
PhoneId = model.PhoneId,
UserId = userId
};
_db.Feedbacks.Add(feedback);
_db.SaveChanges();
Feedback newFeedback = _db.Feedbacks
.Include(f => f.User)
.Include(f => f.Phone)
.First(f => f.Id == feedback.Id);
FeedbackViewModel feedbackViewModel = newFeedback.MapToFeedbackViewModel();
return feedbackViewModel;
}
}
}
\ No newline at end of file
using PhoneStore.ViewModels.Feedback;
namespace PhoneStore.Services.Interfaces
{
public interface IFeedbackService
{
FeedbackViewModel Create(FeedbackCreateViewModel model, int userId);
}
}
\ No newline at end of file
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
<label for="text">Добавьте отзыв о телефоне</label> <label for="text">Добавьте отзыв о телефоне</label>
<input type="text" class="form-control" id="text" placeholder="Минимальная длина отзыва 10 символов"> <input type="text" class="form-control" id="text" placeholder="Минимальная длина отзыва 10 символов">
<input type="text" hidden name="phoneId" id="@Model.Id"> <input type="text" hidden name="phoneId" id="@Model.Id">
<button class="my-3" type="submit">Отправить</button> <button class="my-3 btn btn-outline-warning" type="submit">Отправить</button>
</div> </div>
</form> </form>
} }
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
<a class="nav-link text-dark" asp-area="" asp-controller="Brands" asp-action="Create">Добавить бренд</a> <a class="nav-link text-dark" asp-area="" asp-controller="Brands" asp-action="Create">Добавить бренд</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<input type="text" id="search" class="form-control" placeholder="Критерии для поиска"> <input type="text" id="search" class="form-control" placeholder="Критерии для поиска пользователей">
</li> </li>
</ul> </ul>
</div> </div>
......
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