Exemple #1
0
        /// <summary>
        /// Shows how to create a user, create a jobcode, log time against it, and then run a project report
        /// that shows them
        /// </summary>
        public static void ProjectReportSample()
        {
            var tsheetsApi = new RestClient(_connection, _authProvider);

            DateTime today          = DateTime.Now;
            string   todayString    = today.ToString("yyyy-MM-dd");
            DateTime tomorrow       = today + new TimeSpan(1, 0, 0, 0);
            string   tomorrowString = tomorrow.ToString("yyyy-MM-dd");

            // create a user
            int userId = CreateUser(tsheetsApi);

            // now create a jobcode we can log time against
            int jobCodeId = CreateJobCode(tsheetsApi);

            // log some time
            {
                var     timesheetObjects = new List <JObject>();
                dynamic timesheet        = new JObject();
                timesheet.user_id    = userId;
                timesheet.jobcode_id = jobCodeId;
                timesheet.type       = "manual";
                timesheet.duration   = 3600;
                timesheet.date       = todayString;
                timesheetObjects.Add(timesheet);

                timesheet            = new JObject();
                timesheet.user_id    = userId;
                timesheet.jobcode_id = jobCodeId;
                timesheet.type       = "manual";
                timesheet.duration   = 7200;
                timesheet.date       = todayString;
                timesheetObjects.Add(timesheet);

                var addTimesheetResponse = tsheetsApi.Add(ObjectType.Timesheets, timesheetObjects);
                Console.WriteLine(addTimesheetResponse);

                var addedTimesheets = JObject.Parse(addTimesheetResponse);
            }

            // and run the report
            {
                dynamic reportOptions = new JObject();
                reportOptions.data            = new JObject();
                reportOptions.data.start_date = todayString;
                reportOptions.data.end_date   = tomorrowString;

                var projectReport = tsheetsApi.GetReport(ReportType.Project, reportOptions.ToString());

                Console.WriteLine(projectReport);
            }
        }
Exemple #2
0
        /// <summary>
        /// Helper to create a random job code
        /// </summary>
        private static int CreateJobCode(RestClient tsheetsApi)
        {
            var     jobCodeObjects = new List <JObject>();
            dynamic jobCode        = new JObject();

            jobCode.name            = "jc" + CreateRandomString();
            jobCode.assigned_to_all = true;
            jobCodeObjects.Add(jobCode);

            var addJobCodeResponse = tsheetsApi.Add(ObjectType.Jobcodes, jobCodeObjects);

            Console.WriteLine(addJobCodeResponse);

            // get the job code ID so we can use it later
            var addedJobCode = JObject.Parse(addJobCodeResponse).SelectToken("results.jobcodes.1");

            return((int)addedJobCode["id"]);
        }
Exemple #3
0
        /// <summary>
        /// Helper to create a random user
        /// </summary>
        private static int CreateUser(RestClient tsheetsApi)
        {
            var     userObjects = new List <JObject>();
            dynamic user        = new JObject();

            user.username   = "******" + CreateRandomString();
            user.password   = "******";
            user.first_name = "first";
            user.last_name  = "last";
            userObjects.Add(user);

            var addUserResponse = tsheetsApi.Add(ObjectType.Users, userObjects);

            Console.WriteLine(addUserResponse);

            // get the user ID so we can use it later
            var addedUser = JObject.Parse(addUserResponse).SelectToken("results.users.1");

            return((int)addedUser["id"]);
        }
Exemple #4
0
        /// <summary>
        /// Shows how to add, edit, and delete a timesheet
        /// </summary>
        private static void AddEditDeleteTimesheetSample()
        {
            var tsheetsApi = new RestClient(_connection, _authProvider);

            DateTime today           = DateTime.Now;
            string   todayString     = today.ToString("yyyy-MM-dd");
            DateTime yesterday       = today - new TimeSpan(1, 0, 0, 0);
            string   yesterdayString = yesterday.ToString("yyyy-MM-dd");

            // create a user
            int userId = CreateUser(tsheetsApi);

            // now create a jobcode we can log time against
            int jobCodeId = CreateJobCode(tsheetsApi);

            // add a couple of timesheets
            var     timesheetsToAdd = new List <JObject>();
            dynamic timesheet       = new JObject();

            timesheet.user_id    = userId;
            timesheet.jobcode_id = jobCodeId;
            timesheet.type       = "manual";
            timesheet.duration   = 3600;
            timesheet.date       = todayString;
            timesheetsToAdd.Add(timesheet);

            timesheet            = new JObject();
            timesheet.user_id    = userId;
            timesheet.jobcode_id = jobCodeId;
            timesheet.type       = "manual";
            timesheet.duration   = 7200;
            timesheet.date       = todayString;
            timesheetsToAdd.Add(timesheet);

            var result = tsheetsApi.Add(ObjectType.Timesheets, timesheetsToAdd);

            Console.WriteLine(result);

            // pull out the ids of the new timesheets
            var addedTimesheets = JObject.Parse(result).SelectTokens("results.timesheets.*");
            var timesheetIds    = new List <int>();

            foreach (var ts in addedTimesheets)
            {
                timesheetIds.Add((int)ts["id"]);
            }

            // make some edits
            var timesheetsToEdit = new List <JObject>();

            timesheet      = new JObject();
            timesheet.id   = timesheetIds[0];
            timesheet.date = yesterdayString;
            timesheetsToEdit.Add(timesheet);
            timesheet      = new JObject();
            timesheet.id   = timesheetIds[1];
            timesheet.date = yesterdayString;
            timesheetsToEdit.Add(timesheet);
            result = tsheetsApi.Edit(ObjectType.Timesheets, timesheetsToEdit);
            Console.WriteLine(result);

            // and delete them
            result = tsheetsApi.Delete(ObjectType.Timesheets, timesheetIds);
            Console.WriteLine(result);
        }
Exemple #5
0
 public int CallAdd(int x, int y)
 {
     return(client.Add(x, y));
 }