Beispiel #1
0
        public virtual HttpResponseMessage Delete([FromBody] TimeKeepEntry value)
        {
            try
            {
                if (!ValidateUser(RequestContext.Principal, value))
                {
                    return(Request.CreateCustomErrorResponse(HttpStatusCode.Forbidden, userNotAllowed));
                }
                if (value == null)
                {
                    return(Request.CreateCustomErrorResponse(HttpStatusCode.BadRequest, MissingOrInvalidRequestBody));
                }
                if (!value.ID.HasValue)
                {
                    return(Request.CreateCustomErrorResponse(HttpStatusCode.BadRequest, idIsNull));
                }

                TimeKeepEntry find = TimeKeepEntry.Read(value.ID.Value);
                if (find == null)
                {
                    return(Request.CreateCustomErrorResponse(HttpStatusCode.NotFound, noEntries));
                }

                value.Delete();
                return(Request.CreateResultResponse <TimeKeepEntry>(value));
            }
            catch (Exception ex)
            {
                return(Request.CreateCustomErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Gets the list of time keep entries for a given a case number and time range
        /// </summary>
        /// <param name="user">The case number</param>
        /// <param name="range">The range</param>
        /// <returns>HttpResponseMessage</returns>
        public override HttpResponseMessage GetByCaseTotals(string casenumber, DateRange range)
        {
            try
            {
                if (casenumber == null || casenumber.Trim().Length == 0)
                {
                    return(Request.CreateCustomErrorResponse(HttpStatusCode.BadRequest, CaseNumberIsNull2));
                }
                if (range == null)
                {
                    return(Request.CreateCustomErrorResponse(HttpStatusCode.BadRequest, rangeIsNull));
                }
                IEnumerable <TimeKeepEntry> entries = TimeKeepEntry.ReadByCase(casenumber, GetPrincipalUser(RequestContext.Principal), range);
                TimeSpan totalLabor    = TimeSpan.FromTicks(entries.Sum(c => c.LaborTS.Value.Ticks));
                TimeSpan unloggedLabor = TimeSpan.FromTicks(entries.Where(c => !c.IsLogged).Sum(c => c.LaborTS.Value.Ticks));

                CaseTotalsResponse response = new CaseTotalsResponse {
                    TotalLabor = totalLabor, TotalUnloggedLabor = unloggedLabor
                };

                return(Request.CreateResultResponse <CaseTotalsResponse>(response));
            }
            catch (Exception ex)
            {
                return(Request.CreateCustomErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
Beispiel #3
0
 protected bool ValidateUser(IPrincipal principal, TimeKeepEntry entry)
 {
     if (entry == null)
     {
         return(false);
     }
     return(ValidateUser(principal, entry.User));
 }
Beispiel #4
0
        public virtual HttpResponseMessage Put([FromBody] TimeKeepEntry value)
        {
            try
            {
                if (!ValidateUser(RequestContext.Principal, value))
                {
                    return(Request.CreateCustomErrorResponse(HttpStatusCode.Forbidden, userNotAllowed));
                }
                if (value == null)
                {
                    return(Request.CreateCustomErrorResponse(HttpStatusCode.BadRequest, MissingOrInvalidRequestBody));
                }
                if (!value.ID.HasValue)
                {
                    return(Request.CreateCustomErrorResponse(HttpStatusCode.BadRequest, idIsNull));
                }
                if (value.User == null || value.User.Trim().Length == 0)
                {
                    return(Request.CreateCustomErrorResponse(HttpStatusCode.BadRequest, userIsNull));
                }
                if (value.EndTime.HasValue && value.EndTime.Value <= value.StartTime)
                {
                    return(Request.CreateCustomErrorResponse(HttpStatusCode.BadRequest, EndDateAtOrBeforeStartDate));
                }


                if (value.Category == null)
                {
                    return(Request.CreateCustomErrorResponse(HttpStatusCode.BadRequest, CategoryIsNull));
                }

                if (value.Category.IsScorecard && (string.IsNullOrEmpty(value.CaseNumber) || string.IsNullOrWhiteSpace(value.CaseNumber)))
                {
                    return(Request.CreateCustomErrorResponse(HttpStatusCode.BadRequest, CaseNumberIsNull));
                }

                if (!value.Category.IsScorecard && !string.IsNullOrEmpty(value.CaseNumber) && !string.IsNullOrEmpty(value.CaseNumber))
                {
                    return(Request.CreateCustomErrorResponse(HttpStatusCode.BadRequest, CaseNumberIsNotNull));
                }

                TimeKeepEntry find = TimeKeepEntry.Read(value.ID.Value);
                if (find == null)
                {
                    return(Request.CreateCustomErrorResponse(HttpStatusCode.NotFound, noEntries));
                }

                value.Update();
                return(Request.CreateResultResponse <TimeKeepEntry>(value));
            }
            catch (Exception ex)
            {
                return(Request.CreateCustomErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
Beispiel #5
0
        public virtual HttpResponseMessage Post([FromBody] TimeKeepEntry value)
        {
            try
            {
                if (!ValidateUser(RequestContext.Principal, value))
                {
                    return(Request.CreateCustomErrorResponse(HttpStatusCode.Forbidden, userNotAllowed));
                }
                if (value == null)
                {
                    return(Request.CreateCustomErrorResponse(HttpStatusCode.BadRequest, MissingOrInvalidRequestBody));
                }
                if (value.User == null || value.User.Trim().Length == 0)
                {
                    return(Request.CreateCustomErrorResponse(HttpStatusCode.BadRequest, userIsNull));
                }

                if (value.ID.HasValue)
                {
                    if (value.Category == null)
                    {
                        return(Request.CreateCustomErrorResponse(HttpStatusCode.BadRequest, CategoryIsNull));
                    }

                    if (value.Category.IsScorecard && (string.IsNullOrEmpty(value.CaseNumber) || string.IsNullOrWhiteSpace(value.CaseNumber)))
                    {
                        return(Request.CreateCustomErrorResponse(HttpStatusCode.BadRequest, CaseNumberIsNull));
                    }

                    if (!value.Category.IsScorecard && !string.IsNullOrEmpty(value.CaseNumber) && !string.IsNullOrEmpty(value.CaseNumber))
                    {
                        return(Request.CreateCustomErrorResponse(HttpStatusCode.BadRequest, CaseNumberIsNotNull));
                    }
                }

                TimeKeepEntry modified      = value.Create();
                CreatedResult responseValue = new CreatedResult()
                {
                    Modified = modified,
                    New      = value
                };

                HttpResponseMessage response = Request.CreateResultResponse <CreatedResult>(HttpStatusCode.Created, responseValue);
                response.Headers.Location = new Uri(string.Format("{0}://{1}{2}/timekeepentries/{3}", Request.RequestUri.Scheme,
                                                                  Request.RequestUri.Host, Request.RequestUri.IsDefaultPort ? string.Empty : (":" + Request.RequestUri.Port.ToString()),
                                                                  value.ID));
                return(response);
            }
            catch (Exception ex)
            {
                return(Request.CreateCustomErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
Beispiel #6
0
 public virtual HttpResponseMessage GetByCase(string casenumber, [FromBody] DateRange range)
 {
     try
     {
         if (casenumber == null || casenumber.Trim().Length == 0)
         {
             return(Request.CreateCustomErrorResponse(HttpStatusCode.BadRequest, CaseNumberIsNull2));
         }
         if (range == null)
         {
             return(Request.CreateCustomErrorResponse(HttpStatusCode.BadRequest, rangeIsNull));
         }
         return(Request.CreateResultResponse <IEnumerable <TimeKeepEntry> >(TimeKeepEntry.ReadByCase(casenumber, GetPrincipalUser(RequestContext.Principal), range)));
     }
     catch (Exception ex)
     {
         return(Request.CreateCustomErrorResponse(HttpStatusCode.InternalServerError, ex));
     }
 }
Beispiel #7
0
 public virtual HttpResponseMessage Get(Guid id)
 {
     try
     {
         TimeKeepEntry entry = TimeKeepEntry.Read(id);
         if (!ValidateUser(RequestContext.Principal, entry))
         {
             return(Request.CreateCustomErrorResponse(HttpStatusCode.Forbidden, userNotAllowed));
         }
         if (entry == null)
         {
             return(Request.CreateCustomErrorResponse(HttpStatusCode.NotFound, noEntries));
         }
         return(Request.CreateResultResponse <TimeKeepEntry>(entry));
     }
     catch (Exception ex)
     {
         return(Request.CreateCustomErrorResponse(HttpStatusCode.InternalServerError, ex));
     }
 }
Beispiel #8
0
 public virtual HttpResponseMessage GetByUser(string user, [FromBody] DateRange range)
 {
     try
     {
         if (!ValidateUser(RequestContext.Principal, user))
         {
             return(Request.CreateCustomErrorResponse(HttpStatusCode.Forbidden, userNotAllowed));
         }
         if (user == null || user.Trim().Length == 0)
         {
             return(Request.CreateCustomErrorResponse(HttpStatusCode.BadRequest, userIsNull));
         }
         if (range == null)
         {
             return(Request.CreateCustomErrorResponse(HttpStatusCode.BadRequest, rangeIsNull));
         }
         return(Request.CreateResultResponse <IEnumerable <TimeKeepEntry> >(TimeKeepEntry.ReadByUser(user, range)));
     }
     catch (Exception ex)
     {
         return(Request.CreateCustomErrorResponse(HttpStatusCode.InternalServerError, ex));
     }
 }
Beispiel #9
0
        public virtual HttpResponseMessage GetByUserTotals(string user, [FromBody] DateRange range)
        {
            try
            {
                if (!ValidateUser(RequestContext.Principal, user))
                {
                    return(Request.CreateCustomErrorResponse(HttpStatusCode.Forbidden, userNotAllowed));
                }
                if (user == null || user.Trim().Length == 0)
                {
                    return(Request.CreateCustomErrorResponse(HttpStatusCode.BadRequest, userIsNull));
                }
                if (range == null)
                {
                    return(Request.CreateCustomErrorResponse(HttpStatusCode.BadRequest, rangeIsNull));
                }
                IEnumerable <TimeKeepEntry> entries = TimeKeepEntry.ReadByUser(user, range);
                var validEntries = entries.Where(c => c.EndTime.HasValue && !c.Category.IsOut);

                List <TotalsResult> output = new List <TotalsResult>
                {
                    new TotalsResult
                    {
                        Category = new Category
                        {
                            ID          = Guid.Parse("00000000-0000-0000-0000-000000000000"),
                            Description = "Total Labor",
                            IsOut       = false,
                            IsScorecard = true
                        },
                        TotalLabor = TimeSpan.FromTicks(validEntries.Sum(c => c.LaborTS.Value.Ticks))
                    },

                    new TotalsResult
                    {
                        Category = new Category
                        {
                            ID          = Guid.Parse("00000000-0000-0000-0000-000000000001"),
                            Description = "Scorecard Labor",
                            IsOut       = false,
                            IsScorecard = true
                        },
                        TotalLabor = TimeSpan.FromTicks(validEntries.Where(c => c.Category.IsScorecard).Sum(c => c.LaborTS.Value.Ticks))
                    },

                    new TotalsResult
                    {
                        Category = new Category
                        {
                            ID          = Guid.Parse("00000000-0000-0000-0000-000000000002"),
                            Description = "Non-scorecard Labor",
                            IsOut       = false,
                            IsScorecard = true
                        },
                        TotalLabor = TimeSpan.FromTicks(validEntries.Where(c => !c.Category.IsScorecard).Sum(c => c.LaborTS.Value.Ticks))
                    }
                };
                output.AddRange(validEntries.GroupBy(d => d.Category)
                                .Select(e => new TotalsResult
                {
                    Category   = e.Key,
                    TotalLabor = TimeSpan.FromTicks(e.Sum(f => f.LaborTS.Value.Ticks))
                }).OrderBy(c => c.Category));


                return(Request.CreateResultResponse <IEnumerable <TotalsResult> >(output));
            }
            catch (Exception ex)
            {
                return(Request.CreateCustomErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
Beispiel #10
0
 public Change(string changeType, TimeKeepEntry data)
 {
     this.changeType = changeType;
     this.data       = data;
 }
Beispiel #11
0
 public override HttpResponseMessage Delete([FromBody] TimeKeepEntry value)
 {
     return(base.Delete(value));
 }
Beispiel #12
0
 public override HttpResponseMessage Post([FromBody] TimeKeepEntry value)
 {
     return(base.Post(value));
 }