refactor logic into another habitica service and modified license
This commit is contained in:
43
Habitica.Todoist.Integration.Services/Extensions.cs
Normal file
43
Habitica.Todoist.Integration.Services/Extensions.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using Habitica.Todoist.Integration.Model.Habitica;
|
||||
using Habitica.Todoist.Integration.Model.Habitica.Enums;
|
||||
using Habitica.Todoist.Integration.Model.Todoist;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Habitica.Todoist.Integration.Services.Extensions
|
||||
{
|
||||
public static class Extensions
|
||||
{
|
||||
public static Task ToHabiticaTask(this Item item, string habiticaId = null)
|
||||
{
|
||||
var taskTypeStr = Enum.GetName(typeof(TaskType), TaskType.Todo).ToLower();
|
||||
var task = new Task
|
||||
{
|
||||
Id = habiticaId,
|
||||
Text = item.Content,
|
||||
Type = taskTypeStr,
|
||||
Date = item.Due?.ToJavaScriptDateStr(),
|
||||
Priority = GetHabiticaDifficulty(item.Priority)
|
||||
};
|
||||
|
||||
return task;
|
||||
}
|
||||
|
||||
private static string GetHabiticaDifficulty(int todoistPriority)
|
||||
{
|
||||
switch (todoistPriority)
|
||||
{
|
||||
case 1:
|
||||
return "0.1";
|
||||
case 2:
|
||||
return "1";
|
||||
case 3:
|
||||
return "1.5";
|
||||
case 4:
|
||||
return "2";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
using Habitica.Todoist.Integration.Data;
|
||||
using Habitica.Todoist.Integration.Model.Habitica.Enums;
|
||||
using Habitica.Todoist.Integration.Model.Storage;
|
||||
using Habitica.Todoist.Integration.Model.Todoist;
|
||||
using Habitica.Todoist.Integration.Services.Extensions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Habitica.Todoist.Integration.Services
|
||||
{
|
||||
public class HabiticaIntegrationService
|
||||
{
|
||||
private HabiticaServiceClient habiticaClient { get; set; }
|
||||
private TableStorageClient storageClient { get; set; }
|
||||
|
||||
private string userId { get; set; }
|
||||
|
||||
public HabiticaIntegrationService(string habiticaUserId,
|
||||
string habiticaApiKey,
|
||||
string storageConnectionString,
|
||||
string userId)
|
||||
{
|
||||
this.habiticaClient = new HabiticaServiceClient(habiticaUserId, habiticaApiKey);
|
||||
this.storageClient = new TableStorageClient(storageConnectionString);
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public async Task AddTasks(IEnumerable<Item> items)
|
||||
{
|
||||
foreach (var item in items)
|
||||
await AddTask(item);
|
||||
}
|
||||
|
||||
public async Task AddTask(Item item)
|
||||
{
|
||||
var task = (await habiticaClient.CreateTask(item.ToHabiticaTask())).Data;
|
||||
var link = new TodoHabitLink(userId, item.Id, task.Id);
|
||||
|
||||
await storageClient.InsertOrUpdate(link);
|
||||
await storageClient.InsertOrUpdate(link.Reverse());
|
||||
}
|
||||
|
||||
public async Task UpdateTasks(IEnumerable<Item> items)
|
||||
{
|
||||
foreach (var item in items)
|
||||
await UpdateTask(item);
|
||||
}
|
||||
|
||||
public async Task UpdateTask(Item item)
|
||||
{
|
||||
var habiticaId = (await storageClient.RetrieveRecord<TodoHabitLink>(userId, item.Id)).HabiticaId;
|
||||
await habiticaClient.UpdateTask(item.ToHabiticaTask(habiticaId));
|
||||
}
|
||||
|
||||
public async Task CompleteTasks(IEnumerable<Item> items)
|
||||
{
|
||||
foreach (var item in items)
|
||||
await CompleteTask(item);
|
||||
}
|
||||
|
||||
public async Task CompleteTask(Item item)
|
||||
{
|
||||
await CompleteTask(item.Id);
|
||||
}
|
||||
|
||||
public async Task CompleteTask(string todoistId)
|
||||
{
|
||||
var habiticaId = (await storageClient.RetrieveRecord<TodoHabitLink>(userId, todoistId)).HabiticaId;
|
||||
await habiticaClient.ScoreTask(habiticaId, ScoreAction.Up);
|
||||
}
|
||||
|
||||
public async Task DeleteTasks(IEnumerable<Item> items)
|
||||
{
|
||||
foreach (var item in items)
|
||||
await DeleteTask(item);
|
||||
}
|
||||
|
||||
public async Task DeleteTask(Item item)
|
||||
{
|
||||
await DeleteTask(item.Id);
|
||||
}
|
||||
|
||||
public async Task DeleteTask(string todoistId)
|
||||
{
|
||||
var habiticaId = (await storageClient.RetrieveRecord<TodoHabitLink>(userId, todoistId)).HabiticaId;
|
||||
await habiticaClient.DeleteTask(habiticaId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,12 +18,12 @@ namespace Habitica.Todoist.Integration.Services
|
||||
private string userId { get; set; }
|
||||
private string latestSyncToken { get; set; } = string.Empty;
|
||||
|
||||
public TodoistIntegrationService(TodoistServiceClient todoistClient,
|
||||
TableStorageClient storageClient,
|
||||
public TodoistIntegrationService(string todoistApiKey,
|
||||
string storageConnectionString,
|
||||
string userId)
|
||||
{
|
||||
this.todoistClient = todoistClient;
|
||||
this.storageClient = storageClient;
|
||||
this.todoistClient = new TodoistServiceClient(todoistApiKey);
|
||||
this.storageClient = new TableStorageClient(storageConnectionString);
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user