public async Task <IActionResult> AddAsync(FirmDTO firmDTO)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrorMessage()));
            }
            else
            {
                _mapper = FirmMapping.GetMapper().CreateMapper();
                //Using with Automapper mapped by Firm to FirmDTO
                Firm firm = _mapper.Map <FirmDTO, Firm>(firmDTO);
                //Firm firm = new Firm();

                //firm.FirmName = firmDTO.FirmName;
                //firm.ParentID = firmDTO.ParentID; //instead of this

                var FirmResult = await _Firmservice.AddAsync(firm);

                if (FirmResult.Success)
                {
                    return(Ok(FirmResult.Message));
                }
                else
                {
                    return(BadRequest(FirmResult.Message));
                }
            }
        }
        public async Task <IActionResult> UpdateAsync(FirmDTO firmDTO, int Id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrorMessage()));
            }
            else
            {
                IDataResult <Firm> firmDataResult = await _Firmservice.FindByIdAsync(Id);

                if (firmDataResult.Data == null)
                {
                    return(BadRequest(firmDataResult.Message));
                }
                _mapper = FirmMapping.GetMapper().CreateMapper();
                Firm firm = _mapper.Map <FirmDTO, Firm>(firmDTO);
                firm.ID = firmDataResult.Data.ID;
                IResult firmResult = await _Firmservice.UpdateAsync(firm);

                if (firmResult.Success)
                {
                    return(Ok(firmResult.Message));
                }
                else
                {
                    return(BadRequest(firmResult.Message));
                }
            }
        }
        public IActionResult CreateFirm(FirmDTO model)
        {
            var response = _firmService.CreateFirm(model);

            if (string.IsNullOrEmpty(response.Id))
            {
                return(OK(StatusCodeType.HAS_EXCEPTION, StatusMessage.HAS_EXCEPTION, response));
            }

            return(OK(StatusCodeType.SUCCESS, StatusMessage.SUCCESS, response));
        }
Beispiel #4
0
        public ResponseFirmDTO CreateFirm(FirmDTO model)
        {
            var firmDTO = new ResponseFirmDTO();

            if (model != null)
            {
                var mapperFirm = _mapper.Map <Firm>(model);
                var addedFirm  = _firmRepository.Create(mapperFirm);
                firmDTO = _mapper.Map <ResponseFirmDTO>(addedFirm.Result);
            }
            return(firmDTO);
        }
Beispiel #5
0
        public ActionResult Edit(int firmId)
        {
            FirmDTO dto = new FirmDTO();

            DataEntity firm = DataRepository.Instance.GetById(firmId);

            dto.DataInfo = firm;

            ViewBag.Status = GetStatusToSelect(firm.Status == GenericStatus.ACTIVE ? 1 : 0);

            return(View("Edit", dto));
        }
        public IActionResult Add(FirmDTO firmDto)
        {
            _mapper = FirmMapping.GetMapper().CreateMapper();
            Firm firm = _mapper.Map <FirmDTO, Firm>(firmDto);

            var result = _Firmservice.Add(firm);

            if (result.Success)
            {
                return(Ok(result.Message));
            }

            return(BadRequest(result.Success.ToString() + "and " + result.Message));
        }
Beispiel #7
0
 public void Update(FirmDTO firm)
 {
     try
     {
         var model = Converter.Convert <FirmEntity, FirmDTO>(firm);
         var item  = dbaccess.Firm.Where(x => x.Id == model.Id).FirstOrDefault <FirmEntity>();
         item.Name = model.Name;
         dbaccess.SaveChanges();
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Beispiel #8
0
        public void Upsert(FirmDTO firm)
        {
            try
            {
                var model = Converter.Convert <FirmEntity, FirmDTO>(firm);
                var item  = dbaccess.Firm.Where(x => x.Id == model.Id).FirstOrDefault <FirmEntity>();

                if (item == null)
                {
                    model.InsertDate = DateTime.Now;
                    dbaccess.Firm.Add(model);
                    dbaccess.Firm_User_RL.Add(new Firm_User_RL {
                        FirmId = firm.Id, UserId = firm.UserId
                    });

                    if (firm.UserId != 1)//admin user has to see all firms.....
                    {
                        dbaccess.Firm_User_RL.Add(new Firm_User_RL {
                            FirmId = firm.Id, UserId = 1
                        });
                    }
                }
                else
                {
                    item.Name    = model.Name;
                    item.Contact = model.Contact;
                    item.Address = model.Address;
                    item.PhoneNo = model.PhoneNo;
                }
                dbaccess.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException ex)
            {
                string validationerror = "";
                foreach (var eve in ex.EntityValidationErrors)
                {
                    validationerror = "Entity of type \"" + eve.Entry.Entity.GetType().Name + "\" in state \"" + eve.Entry.State + "\" has the following validation errors:";
                    foreach (var ve in eve.ValidationErrors)
                    {
                        validationerror += "- Property: \"" + ve.PropertyName + "\", Error: \"" + ve.ErrorMessage + "\"";
                    }
                }

                throw new Exception(validationerror);
            }
            catch (System.Data.Entity.Infrastructure.DbUpdateException ex)
            {
                if (ex.InnerException != null)
                {
                    if (ex.InnerException.InnerException != null)
                    {
                        throw new Exception(ex.InnerException.InnerException.Message);
                    }
                    else
                    {
                        throw new Exception(ex.InnerException.Message);
                    }
                }
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
 public void FirmUpdate(FirmDTO firm)
 {
     firmService.Update(firm);
 }
 public void FirmUpsert(FirmDTO firm)
 {
     firmService.Upsert(firm);
 }
Beispiel #11
0
        public ActionResult Save(FirmDTO entity, HttpPostedFileBase logoUpload)
        {
            try
            {
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
                {
                    if (ModelState.IsValid)
                    {
                        ImageEntity imageSaving = new ImageEntity();
                        if (logoUpload != null && logoUpload.ContentLength > 0)
                        {
                            imageSaving.Status    = GenericStatus.ACTIVE;
                            imageSaving.UpdatedBy = CurrentUserId;

                            byte[] cover = null;
                            using (var memoryStream = new MemoryStream())
                            {
                                logoUpload.InputStream.CopyTo(memoryStream);
                                if (memoryStream.Length > 0)
                                {
                                    using (Image image = Bitmap.FromStream(memoryStream))
                                    {
                                        logoUpload.InputStream.CopyTo(memoryStream);
                                        if (memoryStream.Length > 0)
                                        {
                                            cover = memoryStream.ToArray();
                                        }
                                    }
                                }
                            }

                            if (entity.DataInfo.LogoId > 0)
                            {
                                imageSaving.Id = entity.DataInfo.LogoId;
                            }
                            else
                            {
                                imageSaving = ImageRepository.Instance.CreateImage(imageSaving);
                            }

                            ImageRepository.Instance.SaveOrReplaceLogo(imageSaving.Id, cover);

                            entity.DataInfo.LogoId = imageSaving.Id;
                        }

                        entity.DataInfo.UpdatedBy = CurrentUserId;

                        if (entity.DataInfo.Id > 0)
                        {
                            if (entity.Status == 0)
                            {
                                entity.DataInfo.Status = GenericStatus.INACTIVE;
                            }
                            else
                            {
                                entity.DataInfo.Status = GenericStatus.ACTIVE;
                            }

                            ValidateModel(entity.DataInfo);

                            GameEngineDTO game = new GameEngineDTO
                            {
                                Adress       = entity.DataInfo.Adress,
                                City         = entity.DataInfo.City,
                                LogoId       = entity.DataInfo.LogoId,
                                Name         = entity.DataInfo.FirmName,
                                Neighborhood = entity.DataInfo.Neighborhood,
                                Phone        = entity.DataInfo.Phone,
                                Id           = entity.DataInfo.ExternalId,
                                LogoPath     = CurrentURL + entity.DataInfo.LogoId,
                                Description  = entity.DataInfo.Cnpj
                            };

                            try
                            {
                                game = GameEngineService.Instance.CreateOrUpdate(game, "*****@*****.**");
                            }
                            catch (Exception e)
                            {
                                Logger.LogError(e.Message);
                            }


                            List <WorkerDTO> workers = WorkerRepository.Instance.GetAllFromFirm(entity.DataInfo.Id);

                            if (entity.DataInfo.Status == GenericStatus.ACTIVE)
                            {
                                foreach (WorkerDTO item in workers)
                                {
                                    UserAccountEntity acc = AccountRepository.Instance.GetById(item.IdUser);

                                    acc.Status = GenericStatus.ACTIVE;

                                    AccountRepository.Instance.Update(acc);
                                }
                            }
                            else
                            {
                                foreach (WorkerDTO item in workers)
                                {
                                    UserAccountEntity acc = AccountRepository.Instance.GetById(item.IdUser);

                                    acc.Status = GenericStatus.INACTIVE;

                                    AccountRepository.Instance.Update(acc);
                                }
                            }

                            DataRepository.Instance.UpdateFirm(entity.DataInfo);
                        }
                        else
                        {
                            if (!entity.Password.Equals(entity.PasswordConfirmation))
                            {
                                Warning("As duas senhas digitadas não conferem.");
                            }

                            NewRequest request = new NewRequest();

                            AuthResult result = new AuthResult();

                            request.Cpf      = entity.ProfileInfo.CPF;
                            request.Name     = entity.ProfileInfo.Name;
                            request.Phone    = entity.ProfileInfo.Phone;
                            request.Password = entity.Password;
                            request.Email    = entity.ProfileInfo.Email;
                            request.Username = entity.Username;

                            result = AccountHandler.CreateFirmUser(request, Roles.WORKER);

                            if (!AuthStatus.OK.Equals(result.AuthStatus))
                            {
                                Error(AccountHelper.HandleError(result));

                                return(View("Create", entity));
                            }

                            ValidateModel(entity.DataInfo);

                            GameEngineDTO game = new GameEngineDTO
                            {
                                Adress       = entity.DataInfo.Adress,
                                City         = entity.DataInfo.City,
                                LogoId       = entity.DataInfo.LogoId,
                                Name         = entity.DataInfo.FirmName,
                                Neighborhood = entity.DataInfo.Neighborhood,
                                Phone        = entity.DataInfo.Phone,
                                Id           = entity.DataInfo.ExternalId,
                                Description  = entity.DataInfo.Cnpj
                            };
                            game = GameEngineService.Instance.CreateOrUpdate(game, "*****@*****.**");



                            entity.DataInfo.ExternalId = game.Id;

                            entity.DataInfo.Status = GenericStatus.ACTIVE;

                            entity.DataInfo = DataRepository.Instance.CreateFirm(entity.DataInfo);

                            WorkerEntity worker = new WorkerEntity();

                            WorkerTypeEntity workerType = new WorkerTypeEntity();

                            workerType.FirmId      = entity.DataInfo.Id;
                            workerType.ProfileName = Profiles.ADMINISTRADOR;
                            workerType.Status      = GenericStatus.ACTIVE;
                            workerType.TypeName    = "ADMINISTRADOR";
                            workerType.UpdatedBy   = CurrentUserId;

                            workerType = WorkerTypeRepository.Instance.CreateWorkerType(workerType);

                            worker.WorkerTypeId = workerType.Id;
                            worker.UserId       = result.UserId;
                            worker.UpdatedBy    = CurrentUserId;
                            worker.FirmId       = entity.DataInfo.Id;
                            worker.Status       = GenericStatus.ACTIVE;
                            worker.LogoId       = entity.DataInfo.LogoId;

                            PlayerEngineDTO player = new PlayerEngineDTO
                            {
                                Nick   = request.Name,
                                Xp     = 1,
                                Level  = 1,
                                Role   = workerType.TypeName,
                                GameId = game.Id,
                                LogoId = worker.LogoId,
                                Email  = entity.ProfileInfo.Email,
                                Cpf    = entity.ProfileInfo.CPF,
                                Active = true
                            };
                            player = PlayerEngineService.Instance.CreateOrUpdate(player, "*****@*****.**");

                            worker.ExternalId     = player.Id;
                            worker.ExternalFirmId = game.Id;

                            worker = WorkerRepository.Instance.CreateWorker(worker);
                        }

                        Success("Empresa salva com sucesso.");
                        scope.Complete();
                    }
                    else
                    {
                        Warning("Alguns campos são obrigatórios para salvar a empresa.");

                        if (entity.DataInfo.Id > 0)
                        {
                            ViewBag.Status = GetStatusToSelect(entity.DataInfo.Status == GenericStatus.ACTIVE ? 1 : 0);

                            return(View("Edit", entity));
                        }
                        else
                        {
                            return(View("Create", entity));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                Error("Ocorreu um erro ao tentar salvar a empresa.", ex);

                if (entity.DataInfo.Id > 0)
                {
                    ViewBag.Status = GetStatusToSelect(entity.DataInfo.Status == GenericStatus.ACTIVE ? 1 : 0);

                    return(View("Edit", entity));
                }
                else
                {
                    return(View("Create", entity));
                }
            }

            return(View("Index"));
        }
Beispiel #12
0
        public ActionResult Create()
        {
            FirmDTO firm = new FirmDTO();

            return(PartialView("Create", firm));
        }