Skip to content

Commit

Permalink
Added TaskAssignments wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ithielnor committed Dec 29, 2014
1 parent b5e22f0 commit 6f4ece5
Show file tree
Hide file tree
Showing 10 changed files with 301 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Harvest.Net.Tests/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
<add key="Test_PaymentId" value="4270344" />
<add key="Test_ProjectId" value="6710595" />
<add key="Test_ProjectId2" value="6710543" />
<add key="Test_TaskAssignmentId" value="76121786" />
<add key="Test_TaskId" value="3592469" />
<add key="Test_TaskId2" value="3592471" />
<add key="Test_UserAssignmentId" value="53412217" />
<add key="Test_UserId" value="862551" />
</appSettings>
Expand Down
2 changes: 2 additions & 0 deletions Harvest.Net.Tests/FactBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ protected enum TestId
PaymentId,
ProjectId,
ProjectId2,
TaskAssignmentId,
TaskId,
TaskId2,
UserAssignmentId,
UserId,
}
Expand Down
1 change: 1 addition & 0 deletions Harvest.Net.Tests/Harvest.Net.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
</Choose>
<ItemGroup>
<Compile Include="FactBase.cs" />
<Compile Include="ResourceFacts\TaskAssignmentFacts.cs" />
<Compile Include="ResourceFacts\UserAssignmentFacts.cs" />
<Compile Include="ResourceFacts\TimeAndExpenseFacts.cs" />
<Compile Include="ResourceFacts\TaskFacts.cs" />
Expand Down
98 changes: 98 additions & 0 deletions Harvest.Net.Tests/ResourceFacts/TaskAssignmentFacts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using Harvest.Net.Models;
using System;
using System.Linq;
using Xunit;

namespace Harvest.Net.Tests
{
public class TaskAssignmentFacts : FactBase, IDisposable
{
TaskAssignment _todelete = null;
Task _todeletetask = null;

[Fact]
public void ListTaskAssignments_Returns()
{
var list = Api.ListTaskAssignments(GetTestId(TestId.ProjectId));

Assert.True(list != null, "Result list is null.");
Assert.NotEqual(0, list.First().Id);
}

[Fact]
public void TaskAssignment_ReturnsTaskAssignment()
{
var taskAssignment = Api.TaskAssignment(GetTestId(TestId.ProjectId), GetTestId(TestId.TaskAssignmentId));

Assert.NotNull(taskAssignment);
Assert.Equal(GetTestId(TestId.TaskId), taskAssignment.TaskId);
Assert.Equal(GetTestId(TestId.ProjectId), taskAssignment.ProjectId);
}

[Fact]
public void CreateTaskAssignment_ReturnsANewTaskAssignment()
{
_todelete = Api.CreateTaskAssignment(GetTestId(TestId.ProjectId), GetTestId(TestId.TaskId2));

Assert.NotNull(_todelete);
Assert.Equal(GetTestId(TestId.ProjectId), _todelete.ProjectId);
Assert.Equal(GetTestId(TestId.TaskId2), _todelete.TaskId);
}

[Fact]
public void CreateNewTaskAndAssign_ReturnsANewTaskAssignment()
{
_todelete = Api.CreateNewTaskAndAssign(GetTestId(TestId.ProjectId), "Create New Task And Assign Test");
_todeletetask = Api.Task(_todelete.TaskId);

Assert.NotNull(_todelete);
Assert.Equal(GetTestId(TestId.ProjectId), _todelete.ProjectId);
Assert.Equal("Create New Task And Assign Test", _todeletetask.Name);
}

[Fact]
public void DeleteTaskAssignment_ReturnsTrue()
{
var taskAssignment = Api.CreateTaskAssignment(GetTestId(TestId.ProjectId), GetTestId(TestId.TaskId2));

var result = Api.DeleteTaskAssignment(taskAssignment.ProjectId, taskAssignment.Id);

Assert.Equal(true, result);
}

[Fact]
public void UpdateTaskAssignment_UpdatesOnlyChangedValues()
{
var projectId = GetTestId(TestId.ProjectId);

_todelete = Api.CreateTaskAssignment(projectId, GetTestId(TestId.TaskId2));
var updated = Api.UpdateTaskAssignment(projectId, _todelete.Id, billable: true, hourlyRate: 1);

// stuff changed
Assert.NotEqual(_todelete.Billable, updated.Billable);
Assert.Equal(false, _todelete.Billable);
Assert.NotEqual(_todelete.HourlyRate, updated.HourlyRate);
Assert.Equal(null, _todelete.HourlyRate);

// stuff didn't change
Assert.Equal(_todelete.Id, updated.Id);
Assert.Equal(projectId, updated.ProjectId);
Assert.Equal(_todelete.TaskId, updated.TaskId);
Assert.Equal(null, updated.Budget);
Assert.Equal(false, updated.Deactivated);
}

public void Dispose()
{
if (_todelete != null)
{
Api.DeleteTaskAssignment(_todelete.ProjectId, _todelete.Id);
}

if (_todeletetask != null)
{
Api.DeleteTask(_todeletetask.Id);
}
}
}
}
4 changes: 4 additions & 0 deletions Harvest.Net/Harvest.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@
<Compile Include="Models\TaskOptions.cs" />
<Compile Include="Models\Task.cs" />
<Compile Include="Models\AccountUser.cs" />
<Compile Include="Models\TaskAssignment.cs" />
<Compile Include="Models\TaskAssignmentCreateOptions.cs" />
<Compile Include="Models\UserAssignmentCreateOptions.cs" />
<Compile Include="Models\TaskAssignmentOptions.cs" />
<Compile Include="Models\UserAssignmentOptions.cs" />
<Compile Include="Models\UserAssignment.cs" />
<Compile Include="Models\UserOptions.cs" />
Expand All @@ -73,6 +76,7 @@
<Compile Include="Models\InvoiceMessageOptions.cs" />
<Compile Include="Models\InvoiceMessage.cs" />
<Compile Include="Models\OAuth.cs" />
<Compile Include="Resources\TaskAssignment.cs" />
<Compile Include="Resources\UserAssignment.cs" />
<Compile Include="Resources\TimeAndExpense.cs" />
<Compile Include="Resources\Tasks.cs" />
Expand Down
33 changes: 33 additions & 0 deletions Harvest.Net/Models/TaskAssignment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using RestSharp.Serializers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Harvest.Net.Models
{
[SerializeAs(Name = "task-assignment")]
public class TaskAssignment : IModel
{
public long Id { get; set; }

public DateTime CreatedAt { get; set; }

public DateTime UpdatedAt { get; set; }

public long TaskId { get; set; }

public long ProjectId { get; set; }

public bool Billable { get; set; }

public bool Deactivated { get; set; }

public decimal? HourlyRate { get; set; }

public decimal? Budget { get; set; }

public decimal? Estimate { get; set; }
}
}
15 changes: 15 additions & 0 deletions Harvest.Net/Models/TaskAssignmentCreateOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using RestSharp.Serializers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Harvest.Net.Models
{
[SerializeAs(Name = "task")]
internal class TaskAssignmentCreateOptions
{
public long Id { get; set; }
}
}
21 changes: 21 additions & 0 deletions Harvest.Net/Models/TaskAssignmentOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using RestSharp.Serializers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Harvest.Net.Models
{
[SerializeAs(Name = "task-assignment")]
public class TaskAssignmentOptions
{
public bool? Billable { get; set; }

public bool? Deactivated { get; set; }

public decimal? HourlyRate { get; set; }

public decimal? Budget { get; set; }
}
}
125 changes: 125 additions & 0 deletions Harvest.Net/Resources/TaskAssignment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using Harvest.Net.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Harvest.Net
{
public partial class HarvestRestClient
{
// https://github.com/harvesthq/api/blob/master/Sections/Task%20Assignment.md

/// <summary>
/// List all task assignments on a project. Makes a GET request to the Projects/Task_Assignments resource.
/// </summary>
/// <param name="projectId">The Id of the project for which to retrieve assignemnts</param>
/// <param name="updatedSince">An optional filter on the updated-at property</param>
public IList<TaskAssignment> ListTaskAssignments(long projectId, DateTime? updatedSince = null)
{
var request = Request("projects/" + projectId + "/task_assignments");

if (updatedSince != null)
request.AddParameter("updated_since", updatedSince.Value.ToString("yyyy-MM-dd HH:mm"));

return Execute<List<TaskAssignment>>(request);
}

/// <summary>
/// Retrieve a task assignment on a project. Makes a GET request to the Projects/Task_Assignments resource.
/// </summary>
/// <param name="projectId">The Id of the project for which to retrieve an assignemnt</param>
/// <param name="taskAssignmentId">The Id of the assignment to retrieve</param>
public TaskAssignment TaskAssignment(long projectId, long taskAssignmentId)
{
var request = Request("projects/" + projectId + "/task_assignments/" + taskAssignmentId);

return Execute<TaskAssignment>(request);
}

/// <summary>
/// Assign a task to a project. Makes both a POST and a GET request to the Projects/Task_Assignments resource.
/// </summary>
/// <param name="projectId">The Id of the project to which to add the task</param>
/// <param name="taskId">The Id of the task to add</param>
public TaskAssignment CreateTaskAssignment(long projectId, long taskId)
{
var request = Request("projects/" + projectId + "/task_assignments", RestSharp.Method.POST);

request.AddBody(new TaskAssignmentCreateOptions()
{
Id = taskId
});

return Execute<TaskAssignment>(request);
}

/// <summary>
/// Create a new task and assign it to a project. Makes a POST request to the Projects/Task_Assignments/Add_With_Create_New_Task resource, and a GET request to the Projects/Task_Assignments resource.
/// </summary>
/// <param name="projectId">The ID of th project to which to add the new task</param>
/// <param name="name">The name of the new task</param>
public TaskAssignment CreateNewTaskAndAssign(long projectId, string name)
{
var request = Request("projects/" + projectId + "/task_assignments/add_with_create_new_task", RestSharp.Method.POST);

request.AddBody(new TaskOptions()
{
Name = name
});

return Execute<TaskAssignment>(request);
}

/// <summary>
/// Remove a task from a project. Makes a DELETE request to the Projects/Task_Assignments resource.
/// </summary>
/// <param name="projectId">The Id of the project from which to remove the task</param>
/// <param name="taskAssignmentId">The Id of the task assignment to remove</param>
public bool DeleteTaskAssignment(long projectId, long taskAssignmentId)
{
var request = Request("projects/" + projectId + "/task_assignments/" + taskAssignmentId, RestSharp.Method.DELETE);

var result = Execute(request);

return result.StatusCode == System.Net.HttpStatusCode.OK;
}

/// <summary>
/// Update a task assignment on a project. Makes a PUT and a GET request to the Projects/Task_Assignments resource.
/// </summary>
/// <param name="projectId">The ID of the project to update</param>
/// <param name="taskAssignmentId">The ID of the task assignment to update</param>
/// <param name="billable">Whether the task assignment is billable</param>
/// <param name="deactivated">Whether the task assignment is inactive</param>
/// <param name="hourlyRate">The hourly rate</param>
/// <param name="budget">The budget</param>
public TaskAssignment UpdateTaskAssignment(long projectId, long taskAssignmentId, bool? billable = null, bool? deactivated = null, decimal? hourlyRate = null, decimal? budget = null)
{
var options = new TaskAssignmentOptions()
{
Billable = billable,
Deactivated = deactivated,
HourlyRate = hourlyRate,
Budget = budget,
};

return UpdateTaskAssignment(projectId, taskAssignmentId, options);
}

/// <summary>
/// Update a task assignment on a project. Makes a PUT and a GET request to the Projects/Task_Assignments resource.
/// </summary>
/// <param name="projectId">The ID of the project to update</param>
/// <param name="taskAssignmentId">The ID of the task assignment to update</param>
/// <param name="options">The options to be updated</param>
public TaskAssignment UpdateTaskAssignment(long projectId, long taskAssignmentId, TaskAssignmentOptions options)
{
var request = Request("projects/" + projectId + "/task_assignments/" + taskAssignmentId, RestSharp.Method.PUT);

request.AddBody(options);

return Execute<TaskAssignment>(request);
}
}
}
1 change: 0 additions & 1 deletion Harvest.Net/Resources/UserAssignment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public bool DeleteUserAssignment(long projectId, long userAssignmentId)
/// <param name="deactivated">Whether the user assignment is inactive</param>
/// <param name="hourlyRate">The hourly rate</param>
/// <param name="budget">The budget</param>
/// <param name="estimate">The estimate</param>
/// <param name="isProjectManager">Whether this user is a project manager</param>
public UserAssignment UpdateUserAssignment(long projectId, long userAssignmentId, long userId, bool? deactivated = null, decimal? hourlyRate = null, decimal? budget = null, bool? isProjectManager = null)
{
Expand Down

0 comments on commit 6f4ece5

Please sign in to comment.