Пример #1
0
        UpdateKpiAchievementItemResponse UpdateKpiAchievementItem(UpdateKpiAchievementItemRequest request)
        {
            var response = new UpdateKpiAchievementItemResponse();

            try
            {
                var kpiAchievement = request.MapTo <KpiAchievement>();

                if (request.Id != 0)
                {
                    var attachedEntity = DataContext.KpiAchievements.Find(request.Id);
                    if (attachedEntity != null && DataContext.Entry(attachedEntity).State != EntityState.Detached)
                    {
                        DataContext.Entry(attachedEntity).State = EntityState.Detached;
                    }
                    DataContext.KpiAchievements.Attach(kpiAchievement);
                    DataContext.Entry(kpiAchievement).State = EntityState.Modified;
                    DataContext.SaveChanges();
                }
                else
                {
                    kpiAchievement.Kpi = DataContext.Kpis.FirstOrDefault(x => x.Id == request.KpiId);
                    DataContext.KpiAchievements.Add(kpiAchievement);
                    DataContext.SaveChanges();
                }
                response.Id        = kpiAchievement.Id;
                response.IsSuccess = true;
                response.Message   = "KPI Achievement item has been updated successfully";
            }
            catch (InvalidOperationException invalidOperationException)
            {
                response.Message = invalidOperationException.Message;
            }
            catch (ArgumentNullException argumentNullException)
            {
                response.Message = argumentNullException.Message;
            }

            return(response);
        }
Пример #2
0
        public UpdateKpiAchievementItemResponse UpdateKpiAchievementItem(UpdateKpiAchievementItemRequest request)
        {
            var response = new UpdateKpiAchievementItemResponse();

            try
            {
                var user           = DataContext.Users.First(x => x.Id == request.UserId);
                var kpiAchievement = request.MapTo <KpiAchievement>();

                if (request.Id > 0)
                {
                    if (string.IsNullOrEmpty(request.Value) || request.Value == "-" || request.Value.ToLowerInvariant() == "null")
                    {
                        kpiAchievement = DataContext.KpiAchievements.Single(x => x.Id == request.Id);
                        DataContext.KpiAchievements.Remove(kpiAchievement);
                    }
                    else
                    {
                        kpiAchievement = DataContext.KpiAchievements
                                         .Include(x => x.Kpi)
                                         .Include(x => x.UpdatedBy)
                                         .Single(x => x.Id == request.Id);
                        request.MapPropertiesToInstance <KpiAchievement>(kpiAchievement);
                        kpiAchievement.UpdatedBy = user;
                        kpiAchievement.Kpi       = DataContext.Kpis.Single(x => x.Id == request.KpiId);
                    }
                }
                else if (request.Id == 0)
                {
                    if ((string.IsNullOrEmpty(request.Value) || request.Value == "-" ||
                         request.Value.ToLowerInvariant() == "null") && request.Id == 0)
                    {
                        response.IsSuccess = false;
                        response.Message   = "You can not update this item because it is not existed";
                        return(response);
                    }
                    else
                    {
                        kpiAchievement.CreatedBy = user;
                        kpiAchievement.UpdatedBy = user;
                        kpiAchievement.Kpi       = DataContext.Kpis.Single(x => x.Id == request.KpiId);
                        DataContext.KpiAchievements.Add(kpiAchievement);
                    }
                }


                DataContext.SaveChanges();
                response.Id        = request.Id > 0 ? request.Id : kpiAchievement.Id;
                response.IsSuccess = true;
                response.Message   = "KPI Achievement item has been updated successfully";
            }
            catch (InvalidOperationException invalidOperationException)
            {
                response.Message = invalidOperationException.Message;
            }
            catch (ArgumentNullException argumentNullException)
            {
                response.Message = argumentNullException.Message;
            }

            return(response);
        }