File size: 2,707 Bytes
b9c7f0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using FlowAPI.Application.DTOs.Habit;
using FlowAPI.Application.Interfaces;
using FlowAPI.Domain.Entities;

namespace FlowAPI.Application.Services
{
    public class HabitService : IHabitService
    {
        private readonly IHabitRecordRepository _repo;
        private readonly IAchievementService _achievementService;

        public HabitService(IHabitRecordRepository repo, IAchievementService achievementService)
        {
            _repo = repo;
            _achievementService = achievementService;
        }

        public async Task<IEnumerable<HabitRecordResponseDto>> GetAllByUserIdAsync(Guid userId)
        {
            var records = await _repo.GetAllByUserIdAsync(userId);
            return records.Select(MapToDto);
        }

        public async Task<HabitRecordResponseDto> RecordHabitAsync(CreateHabitRecordDto dto)
        {
            var existing = await _repo.GetTodayRecordAsync(dto.UserId, dto.HabitName);
            if (existing is not null) return MapToDto(existing);

            var record = new HabitRecord
            {
                Id = Guid.NewGuid(),
                UserId = dto.UserId,
                HabitName = dto.HabitName,
                CheckedDate = DateTime.UtcNow
            };

            var created = await _repo.CreateAsync(record);
            
            await _achievementService.ProcessEventAsync(dto.UserId, "HabitTracked");
            
            return MapToDto(created);
        }

        public async Task<int> GetStreakAsync(Guid userId, string habitName)
        {
            var records = await _repo.GetAllByUserIdAsync(userId);
            var habitRecords = records
                .Where(r => r.HabitName == habitName)
                .OrderByDescending(r => r.CheckedDate)
                .ToList();

            if (!habitRecords.Any()) return 0;

            int streak = 0;
            DateTime expectedDate = DateTime.UtcNow.Date;

            foreach (var record in habitRecords)
            {
                if (record.CheckedDate.Date == expectedDate)
                {
                    streak++;
                    expectedDate = expectedDate.AddDays(-1);
                }
                else if (record.CheckedDate.Date > expectedDate)
                {
                    continue;
                }
                else
                {
                    break;
                }
            }

            return streak;
        }

        private static HabitRecordResponseDto MapToDto(HabitRecord record) => new()
        {
            Id = record.Id,
            UserId = record.UserId,
            HabitName = record.HabitName,
            CheckedDate = record.CheckedDate
        };
    }
}