/// <summary>
 /// Remover uma funçao
 /// </summary>
 /// <param name="entity"></param>
 /// <returns></returns>
 public void Remove(WorkerTypeMetricEntity workerTypeMetric)
 {
     using (ModelContext context = new ModelContext())
     {
         context.WorkerTypeMetrics.Attach(workerTypeMetric);
         context.WorkerTypeMetrics.Remove(workerTypeMetric);
         context.SaveChanges();
     }
 }
        public ActionResult Remove(int workerTypeMetricId)
        {
            WorkerTypeMetricEntity workerTypeMetric = WorkerTypeMetricRepository.Instance.GetById(workerTypeMetricId);

            workerTypeMetric.Status = GenericStatus.INACTIVE;

            WorkerTypeMetricRepository.Instance.UpdateWorkerTypeMetric(workerTypeMetric);

            return(Redirect("/admin/funcaoMetrica/associar/" + workerTypeMetric.MetricExternalId));
        }
        /// <summary>
        /// Atualiza a associação entre um tipo de funcionario e uma metrica
        /// </summary>
        /// <param name="updatedEntity"></param>
        /// <returns></returns>
        public WorkerTypeMetricEntity UpdateWorkerTypeMetric(WorkerTypeMetricEntity updatedEntity)
        {
            using (ModelContext context = new ModelContext())
            {
                updatedEntity.LastUpdate = DateTime.UtcNow;
                context.WorkerTypeMetrics.Attach(updatedEntity);
                context.Entry(updatedEntity).State = System.Data.Entity.EntityState.Modified;
                context.SaveChanges();
            }

            return(updatedEntity);
        }
        /// <summary>
        /// Salva uma nova associação entre tipo de funcionario e metrica na base de dados
        /// </summary>
        /// <param name="newEntity"></param>
        /// <returns></returns>
        public WorkerTypeMetricEntity CreateWorkerTypeMetric(WorkerTypeMetricEntity newEntity)
        {
            using (ModelContext context = new ModelContext())
            {
                newEntity.MetricId   = 9;
                newEntity.LastUpdate = DateTime.UtcNow;
                context.WorkerTypeMetrics.Attach(newEntity);
                context.Entry(newEntity).State = System.Data.Entity.EntityState.Added;
                context.SaveChanges();
            }

            return(newEntity);
        }
        public ActionResult SaveAssociations(string workerTypesId, string metricId)
        {
            try
            {
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
                {
                    List <string> workerTypesIdList = workerTypesId.Split(',').ToList();

                    foreach (string workerTypeId in workerTypesIdList)
                    {
                        if (!string.IsNullOrWhiteSpace(workerTypeId))
                        {
                            WorkerTypeMetricEntity workerTypeMetric = new WorkerTypeMetricEntity()
                            {
                                UpdatedBy        = CurrentUserId,
                                Status           = GenericStatus.ACTIVE,
                                MetricExternalId = metricId,
                                WorkerTypeId     = int.Parse(workerTypeId)
                            };

                            WorkerTypeMetricRepository.Instance.CreateWorkerTypeMetric(workerTypeMetric);
                        }
                    }

                    Success("Associação feita com sucesso.");
                    scope.Complete();
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                Error("Ocorreu um erro ao tentar associar um tipo de jogador a essa metrica.", ex);
            }

            return(Redirect("/admin/funcaoMetrica/associar/" + metricId));
        }
Ejemplo n.º 6
0
        public ActionResult Save(MetricEngineDTO metric, List <CheckBoxValue> checkBoxes)
        {
            try
            {
                if (metric.GameId == "" || metric.GameId == null)
                {
                    metric.GameId = CurrentFirm.ExternalId;
                }

                if (ModelState.IsValid)
                {
                    ValidateModel(metric);

                    MetricEngineDTO newMetric = MetricEngineService.Instance.CreateOrUpdate(metric);

                    List <int> workerTypeMetrics = WorkerTypeMetricRepository.Instance.GetAllWorkerTypesByMetricId(newMetric.Id).Select(x => x.WorkerTypeId).ToList();

                    foreach (CheckBoxValue checkBox in checkBoxes)
                    {
                        if (checkBox.Checked && !workerTypeMetrics.Contains(checkBox.Value))
                        {
                            WorkerTypeMetricEntity wtm =
                                WorkerTypeMetricRepository.Instance.CreateWorkerTypeMetric(new WorkerTypeMetricEntity
                            {
                                MetricExternalId = newMetric.Id,
                                Status           = GenericStatus.ACTIVE,
                                UpdatedBy        = CurrentUserId,
                                WorkerTypeId     = checkBox.Value
                            });
                        }
                        else if (!checkBox.Checked && workerTypeMetrics.Contains(checkBox.Value))
                        {
                            WorkerTypeMetricEntity toRemove = WorkerTypeMetricRepository.Instance.GetByWorkerTypeIdAndMetricId(checkBox.Value, newMetric.Id);
                            WorkerTypeMetricRepository.Instance.Remove(toRemove);
                        }
                    }

                    if (newMetric == null)
                    {
                        throw new Exception(".");
                    }

                    Success("Metrica atualizada com sucesso.");
                }
                else
                {
                    ViewBag.Icons = Enum.GetValues(typeof(Icons)).Cast <Icons>().Select(i => new SelectListItem
                    {
                        Selected = metric.Icon == i.ToString().Replace("_", "-") ? true : false,
                        Text     = i.GetType().GetMember(i.ToString()).First().GetCustomAttribute <DisplayAttribute>().Name,
                        Value    = i.ToString().Replace("_", "-")
                    }).ToList();

                    ViewBag.WorkerTypes = WorkerTypeRepository.Instance.GetAllByGameId(CurrentFirm.ExternalId);

                    ModelState.AddModelError("", "Alguns campos são obrigatórios para salvar a Métrica.");

                    return(PartialView("_Edit", metric));
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);

                Error("Erro ao atualizar metrica" + ex.Message);
            }

            return(new EmptyResult());
        }