예제 #1
0
        public ApiResponse InsertHourReason(HourReason newHourReason)
        {
            using (var context = new AgmDataContext())
            {
                var email = (Thread.CurrentPrincipal as CustomPrincipal).User.Split('$').GetValue(0) as string;
                var user  = context.Users.Single(u => u.Email == email);

                if (!user.SectionUsersVisible)
                {
                    return(new ApiResponse(false));
                }

                if (context.HourReasons.Any(r => r.Name == newHourReason.Name && !r.IsDeleted))
                {
                    return new ApiResponse(false)
                           {
                               Errors = new ApiResponseError[] { new ApiResponseError()
                                                                 {
                                                                     Message = "Causale già esistente!"
                                                                 } }
                           }
                }
                ;

                if (context.HourReasons.Any(r => r.CodeExport == newHourReason.CodeExport && !r.IsDeleted))
                {
                    return new ApiResponse(false)
                           {
                               Errors = new ApiResponseError[] { new ApiResponseError()
                                                                 {
                                                                     Message = "Codice export causale già utilizzato!"
                                                                 } }
                           }
                }
                ;

                context.HourReasons.Add(newHourReason);
                context.SaveChanges();

                return(new ApiResponse(true));
            }
        }
예제 #2
0
        public ApiResponse UpdateHourReason(HourReason newHourReason)
        {
            if (ModelState.IsValid)
            {
                using (var context = new AgmDataContext())
                {
                    var email = (Thread.CurrentPrincipal as CustomPrincipal).User.Split('$').GetValue(0) as string;

                    var user = context.Users.Single(u => u.Email == email);

                    if (!user.SectionUsersVisible)
                    {
                        return(new ApiResponse(false));
                    }

                    if (!context.HourReasons.Any(r => r.Id == newHourReason.Id))
                    {
                        return new ApiResponse(false)
                               {
                                   Errors =
                                       new ApiResponseError[] { new ApiResponseError()
                                                                {
                                                                    Message = "Causale non esistente!"
                                                                } }
                               }
                    }
                    ;

                    if (context.HourReasons.Any(r => r.Id != newHourReason.Id && r.Name == newHourReason.Name))
                    {
                        return new ApiResponse(false)
                               {
                                   Errors =
                                       new ApiResponseError[]
                                   { new ApiResponseError()
                                     {
                                         Message = "Nome causale già utilizzato!"
                                     } }
                               }
                    }
                    ;

                    if (
                        context.HourReasons.Any(
                            r => r.Id != newHourReason.Id && r.CodeExport == newHourReason.CodeExport))
                    {
                        return new ApiResponse(false)
                               {
                                   Errors =
                                       new ApiResponseError[]
                                   { new ApiResponseError()
                                     {
                                         Message = "Codice export causale già utilizzato!"
                                     } }
                               }
                    }
                    ;

                    context.HourReasons.Attach(newHourReason);
                    ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager.ChangeObjectState(newHourReason,
                                                                                                        EntityState.Modified);
                    context.SaveChanges();

                    return(new ApiResponse(true));
                }
            }
            return(new ApiResponse(false));
        }