240 lines
7.5 KiB
C#
240 lines
7.5 KiB
C#
using ForumContracts.BindingModels;
|
|
using ForumContracts.ViewModels;
|
|
using ForumUserApp.Models;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Diagnostics;
|
|
using static System.Formats.Asn1.AsnWriter;
|
|
|
|
namespace ForumUserApp.Controllers
|
|
{
|
|
public class HomeController : Controller
|
|
{
|
|
private readonly ILogger<HomeController> _logger;
|
|
|
|
public HomeController(ILogger<HomeController> logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
public IActionResult CreateCategory()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[HttpPost]
|
|
public void CreateCategory(string name, string des)
|
|
{
|
|
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(des))
|
|
{
|
|
throw new Exception("Введите название");
|
|
}
|
|
if (string.IsNullOrEmpty(des))
|
|
{
|
|
throw new Exception("Введите описание");
|
|
}
|
|
APIClient.PostRequest("api/main/createcategory", new CategoryBindingModel
|
|
{
|
|
Name = name,
|
|
Description = des
|
|
});
|
|
Response.Redirect("Index");
|
|
}
|
|
|
|
public IActionResult CreateQuestion()
|
|
{
|
|
ViewBag.Categories = APIClient.GetRequest<List<CategoryViewModel>>("api/main/getcategorylist");
|
|
return View();
|
|
}
|
|
|
|
[HttpPost]
|
|
public void CreateQuestion(string questionDes, int categoryId)
|
|
{
|
|
if (string.IsNullOrEmpty(questionDes))
|
|
{
|
|
throw new Exception("Введите вопрос");
|
|
}
|
|
APIClient.PostRequest("api/main/createquestion", new QuestionBindingModel
|
|
{
|
|
UserId = APIClient.User.Id,
|
|
CategoryId=categoryId,
|
|
QuestionDes = questionDes,
|
|
CreateDate = DateTime.Now
|
|
});
|
|
Response.Redirect("Index");
|
|
}
|
|
|
|
[HttpPost]
|
|
public void DeleteQuestion(int questionId)
|
|
{
|
|
APIClient.PostRequest("api/main/deletequestion", new QuestionBindingModel
|
|
{
|
|
Id = questionId
|
|
});
|
|
Response.Redirect("Index");
|
|
}
|
|
|
|
[HttpPost]
|
|
public void DeleteAnswer(int answerId)
|
|
{
|
|
APIClient.PostRequest("api/main/deleteanswer", new AnswerBindingModel
|
|
{
|
|
Id = answerId
|
|
});
|
|
Response.Redirect("Index");
|
|
}
|
|
|
|
public IActionResult Index()
|
|
{
|
|
if (APIClient.User == null)
|
|
{
|
|
return Redirect("~/Home/Enter");
|
|
}
|
|
return View(APIClient.GetRequest<List<CategoryViewModel>>($"api/main/getcategorylist"));
|
|
}
|
|
|
|
public IActionResult Questions(int categoryId)
|
|
{
|
|
if (APIClient.User == null)
|
|
{
|
|
return Redirect("~/Home/Enter");
|
|
}
|
|
return View(APIClient.GetRequest<List<QuestionViewModel>>($"api/main/getquestionsbycategory?categoryId={categoryId}"));
|
|
}
|
|
|
|
public IActionResult UserQuestions()
|
|
{
|
|
if (APIClient.User == null)
|
|
{
|
|
return Redirect("~/Home/Enter");
|
|
}
|
|
return View(APIClient.GetRequest<List<QuestionViewModel>>($"api/main/getquestionsbyuser?userId={APIClient.User.Id}"));
|
|
}
|
|
|
|
public IActionResult HardRequest(DateTime dateFrom,DateTime dateTo)
|
|
{
|
|
if (APIClient.User == null)
|
|
{
|
|
return Redirect("~/Home/Enter");
|
|
}
|
|
return View(APIClient.GetRequest<List<QuestionViewModel>>($"api/main/hardrequest?dateFrom={dateFrom}&dateTo={dateTo}"));
|
|
}
|
|
|
|
public IActionResult CreateAnswer()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[HttpPost]
|
|
public void CreateAnswer(string answerDes, int questionId)
|
|
{
|
|
if (string.IsNullOrEmpty(answerDes))
|
|
{
|
|
throw new Exception("Введите ответ");
|
|
}
|
|
APIClient.PostRequest("api/main/createanswer", new AnswerBindingModel
|
|
{
|
|
UserId = APIClient.User.Id,
|
|
QuestionId = questionId,
|
|
AnswerDes = answerDes,
|
|
ResponseDate = DateTime.Now
|
|
});
|
|
Response.Redirect("Index");
|
|
}
|
|
|
|
public IActionResult Answers(int questionId)
|
|
{
|
|
if (APIClient.User == null)
|
|
{
|
|
return Redirect("~/Home/Enter");
|
|
}
|
|
return View(APIClient.GetRequest<List<AnswerViewModel>>($"api/main/getanswersbyquestion?questionId={questionId}"));
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult Privacy()
|
|
{
|
|
if (APIClient.User == null)
|
|
{
|
|
return Redirect("~/Home/Enter");
|
|
}
|
|
return View(APIClient.User);
|
|
}
|
|
|
|
[HttpPost]
|
|
public void Privacy(string login, string password, string nick)
|
|
{
|
|
if (APIClient.User == null)
|
|
{
|
|
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
|
|
}
|
|
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(nick))
|
|
{
|
|
throw new Exception("Введите логин, пароль и никнейм");
|
|
}
|
|
APIClient.PostRequest("api/user/updatedata", new UserBindingModel
|
|
{
|
|
Id = APIClient.User.Id,
|
|
Nickname= nick,
|
|
Email = login,
|
|
Password = password
|
|
});
|
|
|
|
APIClient.User.Nickname = nick;
|
|
APIClient.User.Email = login;
|
|
APIClient.User.Password = password;
|
|
Response.Redirect("Index");
|
|
}
|
|
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public IActionResult Error()
|
|
{
|
|
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult Enter()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[HttpPost]
|
|
public void Enter(string login, string password)
|
|
{
|
|
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password))
|
|
{
|
|
throw new Exception("Введите логин и пароль");
|
|
}
|
|
APIClient.User = APIClient.GetRequest<UserViewModel>($"api/user/login?login={login}&password={password}");
|
|
if (APIClient.User == null)
|
|
{
|
|
throw new Exception("Неверный логин/пароль");
|
|
}
|
|
Response.Redirect("Index");
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult Register()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[HttpPost]
|
|
public void Register(string login, string password, string nick)
|
|
{
|
|
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(nick))
|
|
{
|
|
throw new Exception("Введите логин, пароль и ник");
|
|
}
|
|
APIClient.PostRequest("api/user/register", new UserBindingModel
|
|
{
|
|
Nickname = nick,
|
|
Email = login,
|
|
Password = password
|
|
});
|
|
Response.Redirect("Enter");
|
|
return;
|
|
}
|
|
}
|
|
}
|