Ejemplo n.º 1
0
        /// <summary>
        ///     Salva o registro passado como parâmetro.
        /// </summary>
        public override PropostaViewModel Add(PropostaViewModel model, bool commit = true)
        {
            var entity = this.mapper.Map <Proposta>(model);

            entity.PropostaTags = new List <PropostaTag>();


            foreach (var propostaTag in model.PropostaTags)
            {
                var tag = this.secaoArquivoTagRepository.GetBy(sa => sa.Tag.Chave == propostaTag.Chave && sa.Tag.Tipo == propostaTag.Tipo).FirstOrDefault();

                if (tag != null)
                {
                    var obj = new PropostaTag();
                    obj.SecaoArquivoTagId = tag.Id;
                    obj.Valor             = propostaTag.Valor;
                    obj.Base64            = propostaTag.Base64;
                    entity.PropostaTags.Add(obj);
                }
            }

            entity.Cliente = this.clienteRepository.GetById(entity.ClienteId);
            entity.Codigo  = Guid.NewGuid();
            entity.Versao  = 1;


            model.Id = this.Add(entity, commit);

            return(model);
        }
Ejemplo n.º 2
0
        public override void Update(PropostaViewModel model, bool commit = true)
        {
            #region Validações

            if (model.Id == 0)
            {
                throw new PropostasException(Messages.NotFound);
            }

            var propostaDb = this.repository.GetById(model.Id);
            if (propostaDb == null)
            {
                throw new PropostasException(Messages.NotFound);
            }

            #endregion Validações

            // Desativando a última proposta para gerar uma nova versão
            propostaDb.Ativo = false;
            this.repository.Update(propostaDb);

            var entity = this.mapper.Map <Proposta>(model);
            entity.PropostaTags = new List <PropostaTag>();

            foreach (var propostaTag in model.PropostaTags)
            {
                var tag = this.secaoArquivoTagRepository.GetBy(sa => sa.Tag.Chave == propostaTag.Chave && sa.Tag.Tipo == propostaTag.Tipo).FirstOrDefault();

                if (tag != null)
                {
                    var obj = new PropostaTag();
                    obj.SecaoArquivoTagId = tag.Id;
                    obj.Valor             = propostaTag.Valor;
                    obj.Base64            = propostaTag.Base64;
                    entity.PropostaTags.Add(obj);
                }
            }


            entity.Id      = 0;
            entity.Cliente = this.clienteRepository.GetById(entity.ClienteId);
            entity.Codigo  = propostaDb.Codigo;
            entity.Versao  = propostaDb.Versao + 1;

            this.Add(entity, commit);
        }