|
1 | 1 | import { Credential, LeetCodeCN } from "leetcode-query"; |
2 | 2 | import { |
3 | 3 | NOTE_AGGREGATE_QUERY, |
4 | | - NOTE_BY_QUESTION_ID_QUERY |
| 4 | + NOTE_BY_QUESTION_ID_QUERY, |
| 5 | + NOTE_CREATE_MUTATION, |
| 6 | + NOTE_UPDATE_MUTATION |
5 | 7 | } from "./graphql/cn/note-queries.js"; |
6 | 8 | import { SEARCH_PROBLEMS_QUERY } from "./graphql/cn/search-problems.js"; |
7 | 9 | import { SOLUTION_ARTICLE_DETAIL_QUERY } from "./graphql/cn/solution-article-detail.js"; |
@@ -463,6 +465,97 @@ export class LeetCodeCNService implements LeetCodeBaseService { |
463 | 465 | } |
464 | 466 | } |
465 | 467 |
|
| 468 | + /** |
| 469 | + * Creates a new note for a specific question on LeetCode CN. |
| 470 | + * Available only on LeetCode CN platform. |
| 471 | + * |
| 472 | + * @param content - The content of the note |
| 473 | + * @param noteType - The type of note (e.g., "COMMON_QUESTION") |
| 474 | + * @param targetId - The ID of the target (e.g., question ID) |
| 475 | + * @param summary - Optional summary of the note |
| 476 | + * @returns Promise resolving to the created note data |
| 477 | + */ |
| 478 | + async createUserNote( |
| 479 | + content: string, |
| 480 | + noteType: string, |
| 481 | + targetId: string, |
| 482 | + summary: string |
| 483 | + ): Promise<any> { |
| 484 | + if (!this.isAuthenticated()) { |
| 485 | + throw new Error("Authentication required to create notes"); |
| 486 | + } |
| 487 | + |
| 488 | + try { |
| 489 | + const variables = { |
| 490 | + content, |
| 491 | + noteType, |
| 492 | + targetId, |
| 493 | + summary: summary || "" |
| 494 | + }; |
| 495 | + |
| 496 | + return await this.leetCodeApi |
| 497 | + .graphql({ |
| 498 | + query: NOTE_CREATE_MUTATION, |
| 499 | + variables |
| 500 | + }) |
| 501 | + .then((response) => { |
| 502 | + return ( |
| 503 | + response.data?.noteCreateCommonNote || { |
| 504 | + ok: false, |
| 505 | + note: null |
| 506 | + } |
| 507 | + ); |
| 508 | + }); |
| 509 | + } catch (error) { |
| 510 | + console.error(`Error creating note:`, error); |
| 511 | + throw error; |
| 512 | + } |
| 513 | + } |
| 514 | + |
| 515 | + /** |
| 516 | + * Updates an existing note on LeetCode CN. |
| 517 | + * Available only on LeetCode CN platform. |
| 518 | + * |
| 519 | + * @param noteId - The ID of the note to update |
| 520 | + * @param content - The new content of the note |
| 521 | + * @param summary - Optional new summary of the note |
| 522 | + * @returns Promise resolving to the updated note data |
| 523 | + */ |
| 524 | + async updateUserNote( |
| 525 | + noteId: string, |
| 526 | + content: string, |
| 527 | + summary: string |
| 528 | + ): Promise<any> { |
| 529 | + if (!this.isAuthenticated()) { |
| 530 | + throw new Error("Authentication required to update notes"); |
| 531 | + } |
| 532 | + |
| 533 | + try { |
| 534 | + const variables = { |
| 535 | + noteId, |
| 536 | + content, |
| 537 | + summary: summary || "" |
| 538 | + }; |
| 539 | + |
| 540 | + return await this.leetCodeApi |
| 541 | + .graphql({ |
| 542 | + query: NOTE_UPDATE_MUTATION, |
| 543 | + variables |
| 544 | + }) |
| 545 | + .then((response) => { |
| 546 | + return ( |
| 547 | + response.data?.noteUpdateUserNote || { |
| 548 | + ok: false, |
| 549 | + note: null |
| 550 | + } |
| 551 | + ); |
| 552 | + }); |
| 553 | + } catch (error) { |
| 554 | + console.error(`Error updating note:`, error); |
| 555 | + throw error; |
| 556 | + } |
| 557 | + } |
| 558 | + |
466 | 559 | isAuthenticated(): boolean { |
467 | 560 | return ( |
468 | 561 | !!this.credential && |
|
0 commit comments