Многие ко многим егор
This commit is contained in:
parent
74537d905a
commit
9da8a0fd79
@ -4,6 +4,9 @@ using System.Net.Http.Headers;
|
||||
using Newtonsoft.Json;
|
||||
using System.Text;
|
||||
using UniversityContracts.ViewModels;
|
||||
using Azure.Core;
|
||||
using UniversityDataModels.Models;
|
||||
using UniversityDatabaseImplement.Models;
|
||||
|
||||
namespace UniversityClientAppStorekeeper
|
||||
{
|
||||
@ -44,5 +47,78 @@ namespace UniversityClientAppStorekeeper
|
||||
throw new Exception(result);
|
||||
}
|
||||
}
|
||||
public static async Task<T?> GetRequestDisciplineAsync<T>(string requestUrl)
|
||||
{
|
||||
var response = await _client.GetAsync(requestUrl);
|
||||
var result = await response.Content.ReadAsStringAsync();
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var settings = new JsonSerializerSettings
|
||||
{
|
||||
Converters = new List<JsonConverter> { new StudentConverter()}
|
||||
};
|
||||
|
||||
return JsonConvert.DeserializeObject<T>(result, settings);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class StudentConverter : JsonConverter
|
||||
{
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return objectType == typeof(IStudentModel);
|
||||
//return true;
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
return null;
|
||||
|
||||
if (existingValue is Student student)
|
||||
return student;
|
||||
|
||||
var student1 = new Student();
|
||||
serializer.Populate(reader, student1);
|
||||
return student1;
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
writer.WriteNull();
|
||||
return;
|
||||
}
|
||||
|
||||
var student = (Student)value;
|
||||
serializer.Serialize(writer, student);
|
||||
}
|
||||
}
|
||||
|
||||
public class DisciplineConverter : JsonConverter
|
||||
{
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return objectType == typeof(IDisciplineModel);
|
||||
//return true;
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
return serializer.Deserialize<Discipline>(reader);
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
serializer.Serialize(writer, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -79,15 +79,21 @@ namespace UniversityClientApp.Controllers
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Disciplines()
|
||||
public async Task<IActionResult> Disciplines()
|
||||
{
|
||||
if (APIStorekeeper.Client == null)
|
||||
{
|
||||
return Redirect("~/Home/Enter");
|
||||
}
|
||||
|
||||
ViewBag.Teachers = APIStorekeeper.GetRequest<List<TeacherViewModel>>($"api/teacher/getteachers?userId={APIStorekeeper.Client.Id}");
|
||||
ViewBag.Students = APIStorekeeper.GetRequest<List<StudentViewModel>>($"api/student/getstudents?userId={APIStorekeeper.Client.Id}");
|
||||
return View(APIStorekeeper.GetRequest<List<DisciplineViewModel>>($"api/discipline/getdisciplines?teacherId={0}"));
|
||||
|
||||
// Îæèäàåì çàâåðøåíèÿ àñèíõðîííîé îïåðàöèè
|
||||
var disciplines = await APIStorekeeper.GetRequestDisciplineAsync<List<DisciplineViewModel>>($"api/discipline/getdisciplines?teacherId={0}");
|
||||
|
||||
// Òåïåðü ìû ìîæåì ïåðåäàòü ðåçóëüòàò â ïðåäñòàâëåíèå
|
||||
return View(disciplines);
|
||||
}
|
||||
[HttpPost]
|
||||
public void Disciplines(string name, string description, DateOnly date, int teacher, List<int> studentIds)
|
||||
@ -112,49 +118,6 @@ namespace UniversityClientApp.Controllers
|
||||
|
||||
Response.Redirect("Disciplines");
|
||||
}
|
||||
/*public IActionResult Disciplines(string name, string description, DateOnly date, int teacher, List<int> studentIds)
|
||||
{
|
||||
if (APIStorekeeper.Client == null)
|
||||
{
|
||||
return RedirectToAction("Enter", "Home");
|
||||
}
|
||||
|
||||
// Ïðåäïîëàãàåì, ÷òî ó âàñ åñòü äîñòóï ê êîíòåêñòó áàçû äàííûõ UniversityDatabase
|
||||
using (var context = new UniversityDatabase())
|
||||
{
|
||||
// Ñîçäàåì ñëîâàðü äëÿ õðàíåíèÿ ñòóäåíòîâ
|
||||
var studentDisciplines = new Dictionary<int, IStudentModel>();
|
||||
|
||||
// Èòåðèðóåì ïî ñïèñêó èäåíòèôèêàòîðîâ ñòóäåíòîâ
|
||||
foreach (var studentId in studentIds)
|
||||
{
|
||||
// Ïîëó÷àåì ñòóäåíòà èç áàçû äàííûõ èëè ñîçäàåì íîâûé ýêçåìïëÿð Student
|
||||
// Âàì íóæíî áóäåò çàïîëíèòü íåîáõîäèìûå ñâîéñòâà, òàêèå êàê UserId, PlanOfStudyId, Name è PhoneNumber
|
||||
var student = context.Students.Find(studentId);
|
||||
|
||||
// Äîáàâëÿåì ñòóäåíòà â ñëîâàðü
|
||||
studentDisciplines.Add(studentId, student);
|
||||
}
|
||||
|
||||
var disciplineModel = new DisciplineBindingModel
|
||||
{
|
||||
UserId = APIStorekeeper.Client.Id,
|
||||
Name = name,
|
||||
Description = description,
|
||||
Date = date,
|
||||
TeacherId = teacher,
|
||||
StudentDisciplines = studentDisciplines
|
||||
};
|
||||
|
||||
APIStorekeeper.PostRequest("api/discipline/creatediscipline", disciplineModel);
|
||||
|
||||
return RedirectToAction("Disciplines");
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
using UniversityDataModels.Models;
|
||||
|
||||
@ -25,5 +26,12 @@ namespace UniversityContracts.ViewModels
|
||||
get;
|
||||
set;
|
||||
} = new();
|
||||
|
||||
public DisciplineViewModel() { }
|
||||
|
||||
[JsonConstructor]
|
||||
public DisciplineViewModel(Dictionary<int, StudentViewModel> disciplineStudents) {
|
||||
this.StudentDisciplines = disciplineStudents.ToDictionary(x => x.Key, x => x.Value as IStudentModel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user