PIbd-21_CourseWork_Polyclin.../Polyclinic/PolyclinicWebAppImplementer/Controllers/RecipeController.cs

73 lines
2.5 KiB
C#

using Microsoft.AspNetCore.Mvc;
using PolyclinicBusinessLogic.BusinessLogics;
using PolyclinicContracts.BindingModels;
using PolyclinicContracts.BusinessLogicsContracts;
using PolyclinicContracts.SearchModels;
using PolyclinicWebAppImplementer.Models;
namespace PolyclinicWebAppImplementer.Controllers
{
public class RecipeController : Controller
{
private readonly IRecipeLogic _recipeLogic;
private readonly ICourseLogic _courseLogic;
public RecipeController(IRecipeLogic recipeLogic, ICourseLogic courseLogic)
{
_recipeLogic = recipeLogic;
_courseLogic = courseLogic;
}
[HttpGet]
[HttpPost]
public IActionResult LinkCourse(RecipeLinkCourseModel model)
{
var recipes = _recipeLogic.ReadList();
var courses = _courseLogic.ReadList();
var linkedRecipes = recipes
.Where(x => x.CourseId != null)
.Select(x => (x, _courseLogic.ReadElement(new CourseSearchModel { Id = x.CourseId }))).ToList();
if (HttpContext.Request.Method == "GET")
{
model = new()
{
Recipes = recipes,
Courses = courses,
LinkedRecipes = linkedRecipes
};
return View(model);
}
else
{
var recipe = _recipeLogic.ReadElement(new RecipeSearchModel { Id = model.RecipeId });
var recipeBindingModel = new RecipeBindingModel
{
Id = recipe.Id,
ProceduresCount = recipe.ProceduresCount,
Comment = recipe.Comment,
RecipeProcedures = recipe.RecipeProcedures,
CourseId = model.CourseId
};
_recipeLogic.Update(recipeBindingModel);
return RedirectToAction("LinkCourse");
}
}
[HttpPost]
public IActionResult UnLinkCourse(int id)
{
var recipe = _recipeLogic.ReadElement(new RecipeSearchModel { Id = id });
var recipeBindingModel = new RecipeBindingModel
{
Id = recipe.Id,
ProceduresCount = recipe.ProceduresCount,
Comment = recipe.Comment,
RecipeProcedures = recipe.RecipeProcedures
};
_recipeLogic.Update(recipeBindingModel);
return RedirectToAction("LinkCourse");
}
}
}