import { Injectable, inject } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable, map } from 'rxjs'; import { Message, Folder, Attachment, SendRequest, BlacklistEntry, PageResponse, Contact } from '../models/mail.models'; @Injectable({ providedIn: 'root' }) export class MailService { private http = inject(HttpClient); private apiUrl = 'http://localhost:8080/api'; // Папки getFolders(): Observable { return this.http.get(`${this.apiUrl}/folders`); } // Письма getMessages(folderId: number, page: number = 0, size: number = 20): Observable> { return this.http.get>(`${this.apiUrl}/messages`, { params: { folderId, page, size } }); } getMessage(id: number): Observable { return this.http.get(`${this.apiUrl}/messages/${id}`); } searchMessages( folderId: number, keyword?: string, subject?: string, body?: string, sender?: string, recipient?: string ): Observable { const params: { [key: string]: string } = { folderId: folderId.toString() }; if (keyword) params['keyword'] = keyword; if (subject) params['subject'] = subject; if (body) params['body'] = body; if (sender) params['sender'] = sender; if (recipient) params['recipient'] = recipient; return this.http.get>(`${this.apiUrl}/messages/search`, { params }) .pipe(map(response => response.content)); } // Отправка send(sendRequest: SendRequest): Observable { return this.http.post(`${this.apiUrl}/send`, sendRequest); } // Статус markAsRead(id: number): Observable { return this.http.put(`${this.apiUrl}/messages/${id}/read`, null); } deleteMessage(id: number): Observable { return this.http.delete(`${this.apiUrl}/messages/${id}`); } // Вложения getAttachments(messageId: number): Observable { return this.http.get(`${this.apiUrl}/messages/${messageId}/attachments`); } uploadAttachment(messageId: number, file: File): Observable { const formData = new FormData(); formData.append('file', file); return this.http.post(`${this.apiUrl}/messages/${messageId}/attachments`, formData); } downloadAttachment(attachmentId: number): Observable { return this.http.get(`${this.apiUrl}/attachments/${attachmentId}/download`, { responseType: 'blob' as 'json' }); } deleteAttachment(attachmentId: number): Observable { return this.http.delete(`${this.apiUrl}/attachments/${attachmentId}`); } // Чёрный список getBlacklist(): Observable { return this.http.get(`${this.apiUrl}/spam/blacklist`); } addToBlacklist(entry: BlacklistEntry): Observable { return this.http.post(`${this.apiUrl}/spam/blacklist`, entry); } removeFromBlacklist(senderEmail: string): Observable { return this.http.delete(`${this.apiUrl}/spam/blacklist`, { params: { senderEmail } }); } // Контакты getContacts(): Observable { return this.http.get(`${this.apiUrl}/contacts`); } addContact(contact: Contact): Observable { return this.http.post(`${this.apiUrl}/contacts`, contact); } updateContact(id: number, contact: Contact): Observable { return this.http.put(`${this.apiUrl}/contacts/${id}`, contact); } deleteContact(id: number): Observable { return this.http.delete(`${this.apiUrl}/contacts/${id}`); } }