Service

QQ 号登记

Target Type Liberal Target

Created 9/12/2024, 7:14:44 PM

Updated 1/1/1, 8:05:43 AM

Description 参加 2nd FDC-TH 的选手均须提供有效的联系方式。使用本服务以登记 QQ 号信息。

Code
1
using Microsoft.Extensions.Logging;
2
using Microsoft.Extensions.DependencyInjection;
3
using PhiZoneApi.Models;
4
using PhiZoneApi.Interfaces;
5
using System.Collections.Generic;
6
using System.Linq;
7
using System;
8
using PhiZoneApi.Dtos.Responses;
9
using PhiZoneApi.Enums;
10

11
var qq = Parameters["QQ 号"];
12
var eventId = new Guid("0a1c0738-48fa-4b4a-9b97-b861a437ae76");
13
await using (var scope = ServiceProvider.CreateAsyncScope())
14
{
15
    var eventTeamRepository = scope.ServiceProvider.GetRequiredService<IEventTeamRepository>();
16

17
    var eventTeams = await eventTeamRepository.GetEventTeamsAsync(predicate: e =>
18
        e.Division.EventId == eventId && e.Participants.Any(f => f.Id == CurrentUser.Id));
19
    if (eventTeams.Count == 0)
20
    {
21
        return new ServiceResponseDto { Type = ServiceResponseType.Error, Message = "请先创建或加入一个队伍!" };
22
    }
23

24
    foreach (var eventTeam in eventTeams)
25
    {
26
        while (eventTeam.Reserved.Count < 3)
27
        {
28
            eventTeam.Reserved.Add(string.Empty);
29
        }
30

31
        var content = eventTeam.Reserved[0] ?? "";
32
        var dict = content.Split('\n')
33
            .Where(e => !string.IsNullOrWhiteSpace(e))
34
            .Select(line => line.Split(":"))
35
            .ToDictionary(parts => int.Parse(parts[0]), parts => parts[1]);
36
        dict[CurrentUser.Id] = $"{CurrentUser.UserName},{qq}";
37
        eventTeam.Reserved[0] = string.Join('\n', dict.Select(pair => $"{pair.Key}:{pair.Value}"));
38
        await eventTeamRepository.UpdateEventTeamAsync(eventTeam);
39
    }
40

41
    return new ServiceResponseDto { Type = ServiceResponseType.Success, Message = "登记成功!" };
42
}