Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
курсач / 1302_3_Курсовая.docx
Скачиваний:
0
Добавлен:
27.12.2025
Размер:
533.61 Кб
Скачать

Листинг программного кода серверной части. Student.Soap.Ts

import { studentService } from '@/student/student.service';

export const soapService = {

StudentService: {

StudentPort: {

getStudent: async function (args: { id: string }) {

const student = await studentService.getStudentById(Number(args.id));

return { student };

},

// НОВЫЙ МЕТОД

getStudents: async function () {

const students = await studentService.getAllStudents();

return { students };

},

createStudent: async function (args: {

name: string;

specialization: string;

course: string;

}) {

const newStudent = await studentService.createStudent({

name: args.name,

specialization: args.specialization,

course: Number(args.course),

});

return { student: newStudent };

},

},

},

};

Листинг программного кода серверной части. Rest.Service.Ts

import type {

Student,

CreateStudentDto,

RequestMetrics,

} from "@/types/student";

const API_URL = "http://localhost:8000/api/students";

// ==========================================

// 1. ХЕЛПЕР ДЛЯ ЗАМЕРОВ (Специфика курсовой)

// ==========================================

async function measure<T>(

label: string,

requestAction: () => Promise<Response>,

): Promise<{ data: T; metrics: RequestMetrics }> {

const startTotal = performance.now();

// 1. СЕТЬ (Скачиваем текст, но не парсим в объект)

const response = await requestAction();

const textData = await response.text();

// 2. ЗАМЕР ПАРСИНГА

const startParse = performance.now();

const jsonData = JSON.parse(textData);

const endParse = performance.now();

const endTotal = performance.now();

const rawString = JSON.stringify(jsonData, null, 2);

return {

data: jsonData,

metrics: {

method: label,

protocol: "REST",

duration: endTotal - startTotal,

parsingTime: endParse - startParse,

dataSize: rawString.length,

rawResponse: rawString,

},

};

}

// ==========================================

// 2. ЭКСПОРТ ДЛЯ UI (Адаптер)

// ==========================================

/*

App.vue импортирует именно это.

ServicePanel получит то, что ждет (данные + метрики),

а внутри мы вызываем чистый pureRestService.

*/

export const restService = {

async getAll() {

return measure("Get List", () => fetch(API_URL));

},

async create(student: CreateStudentDto) {

return measure("Create", () =>

fetch(API_URL, {

method: "POST",

headers: { "Content-Type": "application/json" },

body: JSON.stringify(student),

}),

);

},

};

Листинг программного кода серверной части. Soap.Service.Ts

import type {

Student,

CreateStudentDto,

RequestMetrics,

} from "@/types/student";

const SOAP_URL = "http://localhost:8000/soap";

async function measure<T>(

label: string,

xmlBody: string,

parseLogic: (rawXml: string) => T,

): Promise<{ data: T; metrics: RequestMetrics }> {

const startTotal = performance.now();

const response = await fetch(SOAP_URL, {

method: "POST",

headers: { "Content-Type": "text/xml" },

body: xmlBody,

});

const textResponse = await response.text();

const startParse = performance.now();

const parsedData = parseLogic(textResponse);

const endParse = performance.now();

const endTotal = performance.now();

return {

data: parsedData,

metrics: {

method: label,

protocol: "SOAP",

duration: endTotal - startTotal,

parsingTime: endParse - startParse,

dataSize: textResponse.length,

rawResponse: textResponse,

},

};

}

export const soapService = {

async getAll() {

const xml = `

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://www.examples.com/wsdl/StudentService.wsdl">

<soapenv:Header/>

<soapenv:Body><tns:getStudentsRequest/></soapenv:Body>

</soapenv:Envelope>`;

return measure<Student[]>("Get List", xml, (rawXml) => {

const parser = new DOMParser();

const doc = parser.parseFromString(rawXml, "text/xml");

const nodes = doc.getElementsByTagName("students");

const students: Student[] = [];

for (let i = 0; i < nodes.length; i++) {

const node = nodes[i];

if (!node) continue;

const getId = (tag: string) =>

node.getElementsByTagName(tag)[0]?.textContent;

students.push({

id: Number(getId("id") || 0),

name: getId("name") || "",

specialization: getId("specialization") || "",

course: Number(getId("course") || 0),

});

}

return students;

});

},

async create(student: CreateStudentDto) {

const xml = `

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://www.examples.com/wsdl/StudentService.wsdl">

<soapenv:Header/>

<soapenv:Body>

<tns:createStudentRequest>

<tns:name>${student.name}</tns:name>

<tns:specialization>${student.specialization}</tns:specialization>

<tns:course>${student.course}</tns:course>

</tns:createStudentRequest>

</soapenv:Body>

</soapenv:Envelope>`;

return measure<any>("Create", xml, (rawXml) => {

return rawXml;

});

},

};

Соседние файлы в папке курсач