Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
do not display days if the time is negative (currently toggling)
  • Loading branch information
Nasicus committed May 1, 2015
1 parent fdf5ab4 commit ae27dc6
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 72 deletions.
2 changes: 1 addition & 1 deletion ClientApp/App/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
success(data);
})
.error(function(data, status) {
fail(status, data.ExceptionMessage);
fail(status, data.Message);
});
}

Expand Down
4 changes: 2 additions & 2 deletions ClientApp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ <h1>Toggl Overtime</h1>

<div class="resultsDiv" data-ng-hide="!hasError">
<div data-nc-row-center data-additional-class="errorDiv">
<h2>Oh no - an error occured :(</h2>
<b>{{errorStatus}}</b>: {{errorMessage}}
<h2>An error occured :(</h2>
{{errorMessage}}
</div>
</div>

Expand Down
164 changes: 95 additions & 69 deletions Source/ApiControllers/DataController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Nasicus.Toggl.Overtime.App_Start;
using Nasicus.Toggl.Overtime.Model;
Expand All @@ -11,73 +14,96 @@

namespace Nasicus.Toggl.Overtime.ApiControllers
{
public class DataController : ApiController
{
[HttpGet]
public TogglTimeSummary Get(string apiToken, string regularWorkingHoursString, string startDateString, string endDateString)
{
double regularWorkingHours = Double.Parse(regularWorkingHoursString);
DateTime startDate = DateTime.ParseExact(startDateString, WebApiConfig.DateFormat, CultureInfo.InvariantCulture);
DateTime endDate = DateTime.ParseExact(endDateString, WebApiConfig.DateFormat, CultureInfo.InvariantCulture);

double workDayInSeconds = (regularWorkingHours/ 5.0 * 3600);

var togglTimeSummary = new TogglTimeSummary();

var timeService = new TimeEntryService(apiToken);
var timeParams = new TimeEntryParams
{
StartDate = startDate,
EndDate = endDate.AddDays(1)
};

double overTime = 0;
double workTime = 0;
WeekSummary weekSummary = null;

foreach (IGrouping<DateTime, TimeEntry> dayEntry in timeService.List(timeParams)
.GroupBy(h => DateTime.Parse(h.Start).Date)
.OrderByDescending(d => d.Key))
{
var daySummary = new DaySummary(dayEntry.Key);
long currentDuration = 0;

string currentYearAndWeek = String.Format("{0}-{1}",
daySummary.Date.Year,
DateTimeUtility.GetIso8601WeekOfYear(daySummary.Date));

if (weekSummary == null || currentYearAndWeek != weekSummary.DisplayName)
{
weekSummary = new WeekSummary(currentYearAndWeek, workDayInSeconds);
togglTimeSummary.AddWeek(weekSummary);
}

weekSummary.AddDay(daySummary);

foreach (long duration in dayEntry.Where(t => t.Duration != null).Select(timeEntry => (long)timeEntry.Duration))
{
daySummary.AddTimeEntry(duration);
currentDuration += duration;
}

daySummary.Worktime = currentDuration;

if (currentDuration <= 0)
{
continue;
}

daySummary.Overtime = currentDuration - workDayInSeconds;
weekSummary.Worktime += currentDuration;
overTime += daySummary.Overtime;
workTime += daySummary.Worktime;
weekSummary.Overtime += daySummary.Overtime;
}

togglTimeSummary.Overtime = overTime;
togglTimeSummary.Worktime = workTime;

return togglTimeSummary;
}
}
public class DataController : ApiController
{
[HttpGet]
public HttpResponseMessage Get(string apiToken, string regularWorkingHoursString, string startDateString,
string endDateString)
{
double regularWorkingHours;
DateTime startDate;
DateTime endDate;
try
{
regularWorkingHours = double.Parse(regularWorkingHoursString);
startDate = DateTime.ParseExact(startDateString, WebApiConfig.DateFormat, CultureInfo.InvariantCulture);
endDate = DateTime.ParseExact(endDateString, WebApiConfig.DateFormat, CultureInfo.InvariantCulture);
}
catch (Exception)
{
return Request.CreateErrorResponse(HttpStatusCode.PreconditionFailed, "At least one of the passed parameters was not in the correct format!");
}

double workDayInSeconds = (regularWorkingHours/5.0*3600);

var togglTimeSummary = new TogglTimeSummary();

var timeService = new TimeEntryService(apiToken);
var timeParams = new TimeEntryParams
{
StartDate = startDate,
EndDate = endDate.AddDays(1)
};

double overTime = 0;
double workTime = 0;
WeekSummary weekSummary = null;

Dictionary<DateTime, List<TimeEntry>> orderByDescending;
try
{
orderByDescending = timeService.List(timeParams)
.GroupBy(h => DateTime.Parse(h.Start).Date)
.OrderByDescending(d => d.Key).ToDictionary(kvp => kvp.Key, kvp => kvp.ToList());
}
catch (WebException webEx)
{
HttpWebResponse httpWebResponse = ((HttpWebResponse)webEx.Response);
HttpStatusCode statusCode = httpWebResponse.StatusCode;
string statusDescription = httpWebResponse.StatusDescription;
return Request.CreateErrorResponse(HttpStatusCode.PreconditionFailed,
$"The toggl server (toggl.com) threw an exception ({(int)statusCode}: {statusDescription}) - either your API token is wrong or the servers are down.");
}

foreach (KeyValuePair<DateTime, List<TimeEntry>> dayEntry in orderByDescending)
{
DaySummary daySummary = new DaySummary(dayEntry.Key);
long currentDuration = 0;

string currentYearAndWeek = $"{daySummary.Date.Year}-{DateTimeUtility.GetIso8601WeekOfYear(daySummary.Date)}";

if (weekSummary == null || currentYearAndWeek != weekSummary.DisplayName)
{
weekSummary = new WeekSummary(currentYearAndWeek, workDayInSeconds);
togglTimeSummary.AddWeek(weekSummary);
}

foreach (long duration in dayEntry.Value.Where(t => t.Duration != null).Select(timeEntry => (long) timeEntry.Duration))
{
daySummary.AddTimeEntry(duration);
currentDuration += duration;
}

daySummary.Worktime = currentDuration;

if (currentDuration <= 0)
{
continue;
}

weekSummary.AddDay(daySummary);

daySummary.Overtime = currentDuration - workDayInSeconds;
weekSummary.Worktime += currentDuration;
overTime += daySummary.Overtime;
workTime += daySummary.Worktime;
weekSummary.Overtime += daySummary.Overtime;
}

togglTimeSummary.Overtime = overTime;
togglTimeSummary.Worktime = workTime;

return Request.CreateResponse(HttpStatusCode.OK, togglTimeSummary);
}
}
}
1 change: 1 addition & 0 deletions Toggl.Overtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
</ItemGroup>
<ItemGroup>
<Content Include="packages.config" />
<None Include="Properties\PublishProfiles\doxpres.pubxml" />
<None Include="Properties\PublishProfiles\togglDox.pubxml" />
<None Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
Expand Down

0 comments on commit ae27dc6

Please sign in to comment.