Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 0 additions & 128 deletions src/Orbit.Api/Controllers/HabitsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
using Orbit.Api.Extensions;
using Orbit.Application.Habits.Commands;
using Orbit.Application.Habits.Queries;
using Orbit.Domain.Enums;
using Orbit.Domain.Interfaces;
using Orbit.Domain.ValueObjects;

#pragma warning disable CA1873

Expand All @@ -17,132 +15,6 @@ namespace Orbit.Api.Controllers;
[Route("api/[controller]")]
public partial class HabitsController(IMediator mediator, ILogger<HabitsController> logger, IUserDateService userDateService) : ControllerBase
{
public record CreateHabitRequest(
string Title,
string? Description,
FrequencyUnit? FrequencyUnit,
int? FrequencyQuantity,
IReadOnlyList<System.DayOfWeek>? Days = null,
bool IsBadHabit = false,
IReadOnlyList<string>? SubHabits = null,
DateOnly? DueDate = null,
TimeOnly? DueTime = null,
TimeOnly? DueEndTime = null,
bool ReminderEnabled = false,
IReadOnlyList<int>? ReminderTimes = null,
IReadOnlyList<ScheduledReminderTime>? ScheduledReminders = null,
bool SlipAlertEnabled = false,
IReadOnlyList<Guid>? TagIds = null,
IReadOnlyList<ChecklistItem>? ChecklistItems = null,
bool IsGeneral = false,
DateOnly? EndDate = null,
bool IsFlexible = false,
IReadOnlyList<Guid>? GoalIds = null,
string? Emoji = null);

public record UpdateHabitRequest(
string Title,
string? Description,
FrequencyUnit? FrequencyUnit,
int? FrequencyQuantity,
IReadOnlyList<System.DayOfWeek>? Days = null,
bool IsBadHabit = false,
DateOnly? DueDate = null,
TimeOnly? DueTime = null,
TimeOnly? DueEndTime = null,
bool? ReminderEnabled = null,
IReadOnlyList<int>? ReminderTimes = null,
IReadOnlyList<ScheduledReminderTime>? ScheduledReminders = null,
bool? SlipAlertEnabled = null,
IReadOnlyList<ChecklistItem>? ChecklistItems = null,
bool? IsGeneral = null,
DateOnly? EndDate = null,
bool? ClearEndDate = null,
bool? IsFlexible = null,
IReadOnlyList<Guid>? GoalIds = null,
string? Emoji = null);

public record UpdateChecklistRequest(IReadOnlyList<ChecklistItem> ChecklistItems);

public record LogHabitRequest(DateOnly? Date = null);

public record SkipHabitRequest(DateOnly? Date = null);

public record BulkCreateHabitsRequest(
IReadOnlyList<BulkHabitItemRequest> Habits,
bool FromSyncReview = false);

public record BulkHabitItemRequest(
string Title,
string? Description,
FrequencyUnit? FrequencyUnit,
int? FrequencyQuantity,
IReadOnlyList<System.DayOfWeek>? Days = null,
bool IsBadHabit = false,
DateOnly? DueDate = null,
TimeOnly? DueTime = null,
TimeOnly? DueEndTime = null,
bool ReminderEnabled = false,
IReadOnlyList<int>? ReminderTimes = null,
IReadOnlyList<ScheduledReminderTime>? ScheduledReminders = null,
IReadOnlyList<BulkHabitItemRequest>? SubHabits = null,
bool IsGeneral = false,
DateOnly? EndDate = null,
bool IsFlexible = false,
IReadOnlyList<ChecklistItem>? ChecklistItems = null,
string? GoogleEventId = null,
string? Emoji = null);

public record BulkDeleteHabitsRequest(IReadOnlyList<Guid> HabitIds);

public record BulkLogHabitItem(Guid HabitId, DateOnly? Date = null);
public record BulkLogHabitsRequest(IReadOnlyList<BulkLogHabitItem> Items);

public record BulkSkipHabitItem(Guid HabitId, DateOnly? Date = null);
public record BulkSkipHabitsRequest(IReadOnlyList<BulkSkipHabitItem> Items);

public record ReorderHabitsRequest(IReadOnlyList<HabitPositionRequest> Positions);

public record HabitPositionRequest(Guid HabitId, int Position);

public record MoveHabitParentRequest(Guid? ParentId);

public record GetHabitsFilterRequest
{
public DateOnly? DateFrom { get; init; }
public DateOnly? DateTo { get; init; }
public bool? IncludeOverdue { get; init; }
public string? Search { get; init; }
public string? FrequencyUnit { get; init; }
public bool? IsCompleted { get; init; }
public Guid[]? TagIds { get; init; }
public bool? IsGeneral { get; init; }
public int Page { get; init; } = 1;
public int PageSize { get; init; } = 50;
public bool? IncludeGeneral { get; init; }
}

public record CreateSubHabitRequest(
string Title,
string? Description = null,
FrequencyUnit? FrequencyUnit = null,
int? FrequencyQuantity = null,
IReadOnlyList<System.DayOfWeek>? Days = null,
bool IsBadHabit = false,
DateOnly? DueDate = null,
TimeOnly? DueTime = null,
TimeOnly? DueEndTime = null,
bool ReminderEnabled = false,
IReadOnlyList<int>? ReminderTimes = null,
IReadOnlyList<ScheduledReminderTime>? ScheduledReminders = null,
bool SlipAlertEnabled = false,
IReadOnlyList<ChecklistItem>? ChecklistItems = null,
IReadOnlyList<Guid>? TagIds = null,
DateOnly? EndDate = null,
bool IsFlexible = false,
string? Emoji = null);
public record LinkGoalsRequest(List<Guid> GoalIds);

[HttpGet("count")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
Expand Down
134 changes: 134 additions & 0 deletions src/Orbit.Api/Controllers/HabitsControllerRequests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using Orbit.Domain.Enums;
using Orbit.Domain.ValueObjects;

namespace Orbit.Api.Controllers;

public partial class HabitsController
{
public record CreateHabitRequest(
string Title,
string? Description,
FrequencyUnit? FrequencyUnit,
int? FrequencyQuantity,
IReadOnlyList<System.DayOfWeek>? Days = null,
bool IsBadHabit = false,
IReadOnlyList<string>? SubHabits = null,
DateOnly? DueDate = null,
TimeOnly? DueTime = null,
TimeOnly? DueEndTime = null,
bool ReminderEnabled = false,
IReadOnlyList<int>? ReminderTimes = null,
IReadOnlyList<ScheduledReminderTime>? ScheduledReminders = null,
bool SlipAlertEnabled = false,
IReadOnlyList<Guid>? TagIds = null,
IReadOnlyList<ChecklistItem>? ChecklistItems = null,
bool IsGeneral = false,
DateOnly? EndDate = null,
bool IsFlexible = false,
IReadOnlyList<Guid>? GoalIds = null,
string? Emoji = null);

public record UpdateHabitRequest(
string Title,
string? Description,
FrequencyUnit? FrequencyUnit,
int? FrequencyQuantity,
IReadOnlyList<System.DayOfWeek>? Days = null,
bool IsBadHabit = false,
DateOnly? DueDate = null,
TimeOnly? DueTime = null,
TimeOnly? DueEndTime = null,
bool? ReminderEnabled = null,
IReadOnlyList<int>? ReminderTimes = null,
IReadOnlyList<ScheduledReminderTime>? ScheduledReminders = null,
bool? SlipAlertEnabled = null,
IReadOnlyList<ChecklistItem>? ChecklistItems = null,
bool? IsGeneral = null,
DateOnly? EndDate = null,
bool? ClearEndDate = null,
bool? IsFlexible = null,
IReadOnlyList<Guid>? GoalIds = null,
string? Emoji = null);

public record UpdateChecklistRequest(IReadOnlyList<ChecklistItem> ChecklistItems);

public record LogHabitRequest(DateOnly? Date = null);

public record SkipHabitRequest(DateOnly? Date = null);

public record BulkCreateHabitsRequest(
IReadOnlyList<BulkHabitItemRequest> Habits,
bool FromSyncReview = false);

public record BulkHabitItemRequest(
string Title,
string? Description,
FrequencyUnit? FrequencyUnit,
int? FrequencyQuantity,
IReadOnlyList<System.DayOfWeek>? Days = null,
bool IsBadHabit = false,
DateOnly? DueDate = null,
TimeOnly? DueTime = null,
TimeOnly? DueEndTime = null,
bool ReminderEnabled = false,
IReadOnlyList<int>? ReminderTimes = null,
IReadOnlyList<ScheduledReminderTime>? ScheduledReminders = null,
IReadOnlyList<BulkHabitItemRequest>? SubHabits = null,
bool IsGeneral = false,
DateOnly? EndDate = null,
bool IsFlexible = false,
IReadOnlyList<ChecklistItem>? ChecklistItems = null,
string? GoogleEventId = null,
string? Emoji = null);

public record BulkDeleteHabitsRequest(IReadOnlyList<Guid> HabitIds);

public record BulkLogHabitItem(Guid HabitId, DateOnly? Date = null);
public record BulkLogHabitsRequest(IReadOnlyList<BulkLogHabitItem> Items);

public record BulkSkipHabitItem(Guid HabitId, DateOnly? Date = null);
public record BulkSkipHabitsRequest(IReadOnlyList<BulkSkipHabitItem> Items);

public record ReorderHabitsRequest(IReadOnlyList<HabitPositionRequest> Positions);

public record HabitPositionRequest(Guid HabitId, int Position);

public record MoveHabitParentRequest(Guid? ParentId);

public record GetHabitsFilterRequest
{
public DateOnly? DateFrom { get; init; }
public DateOnly? DateTo { get; init; }
public bool? IncludeOverdue { get; init; }
public string? Search { get; init; }
public string? FrequencyUnit { get; init; }
public bool? IsCompleted { get; init; }
public Guid[]? TagIds { get; init; }
public bool? IsGeneral { get; init; }
public int Page { get; init; } = 1;
public int PageSize { get; init; } = 50;
public bool? IncludeGeneral { get; init; }
}

public record CreateSubHabitRequest(
string Title,
string? Description = null,
FrequencyUnit? FrequencyUnit = null,
int? FrequencyQuantity = null,
IReadOnlyList<System.DayOfWeek>? Days = null,
bool IsBadHabit = false,
DateOnly? DueDate = null,
TimeOnly? DueTime = null,
TimeOnly? DueEndTime = null,
bool ReminderEnabled = false,
IReadOnlyList<int>? ReminderTimes = null,
IReadOnlyList<ScheduledReminderTime>? ScheduledReminders = null,
bool SlipAlertEnabled = false,
IReadOnlyList<ChecklistItem>? ChecklistItems = null,
IReadOnlyList<Guid>? TagIds = null,
DateOnly? EndDate = null,
bool IsFlexible = false,
string? Emoji = null);

public record LinkGoalsRequest(List<Guid> GoalIds);
}
77 changes: 0 additions & 77 deletions src/Orbit.Api/Controllers/SyncController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,83 +311,6 @@ await strategy.ExecuteAsync(async () =>
}
}

private async Task ProcessMutation(Guid userId, SyncMutation mutation, CancellationToken ct)
{
switch (mutation.Entity.ToLowerInvariant())
{
case "habit":
await ProcessHabitMutation(userId, mutation, ct);
break;
case "goal":
await ProcessGoalMutation(userId, mutation, ct);
break;
case "tag":
await ProcessTagMutation(userId, mutation, ct);
break;
case "notification":
await ProcessNotificationMutation(userId, mutation, ct);
break;
default:
throw new InvalidOperationException($"Unknown entity type: {mutation.Entity}");
}
}

private async Task ProcessHabitMutation(Guid userId, SyncMutation mutation, CancellationToken ct)
{
switch (mutation.Action.ToLowerInvariant())
{
case "delete":
if (mutation.Id is null) throw new InvalidOperationException("Id is required for delete.");
var habit = await dbContext.Habits.FirstOrDefaultAsync(h => h.Id == mutation.Id && h.UserId == userId, ct);
if (habit is not null) habit.SoftDelete();
break;
default:
throw new InvalidOperationException($"Unsupported action: {mutation.Action} for habit.");
}
}

private async Task ProcessGoalMutation(Guid userId, SyncMutation mutation, CancellationToken ct)
{
switch (mutation.Action.ToLowerInvariant())
{
case "delete":
if (mutation.Id is null) throw new InvalidOperationException("Id is required for delete.");
var goal = await dbContext.Goals.FirstOrDefaultAsync(g => g.Id == mutation.Id && g.UserId == userId, ct);
if (goal is not null) goal.SoftDelete();
break;
default:
throw new InvalidOperationException($"Unsupported action: {mutation.Action} for goal.");
}
}

private async Task ProcessTagMutation(Guid userId, SyncMutation mutation, CancellationToken ct)
{
switch (mutation.Action.ToLowerInvariant())
{
case "delete":
if (mutation.Id is null) throw new InvalidOperationException("Id is required for delete.");
var tag = await dbContext.Tags.FirstOrDefaultAsync(t => t.Id == mutation.Id && t.UserId == userId, ct);
if (tag is not null) tag.SoftDelete();
break;
default:
throw new InvalidOperationException($"Unsupported action: {mutation.Action} for tag.");
}
}

private async Task ProcessNotificationMutation(Guid userId, SyncMutation mutation, CancellationToken ct)
{
switch (mutation.Action.ToLowerInvariant())
{
case "read":
if (mutation.Id is null) throw new InvalidOperationException("Id is required for read.");
var notification = await dbContext.Notifications.FirstOrDefaultAsync(n => n.Id == mutation.Id && n.UserId == userId, ct);
if (notification is not null) notification.MarkAsRead();
break;
default:
throw new InvalidOperationException($"Unsupported action: {mutation.Action} for notification.");
}
}

private static SyncHabitDto MapHabit(Habit habit)
{
return new SyncHabitDto(
Expand Down
Loading
Loading