public async Task <Response <PlanoTreino> > SalvarAsync(PlanoTreino planoTreino)
        {
            var resp = new Response <PlanoTreino>();

            if (planoTreino == null)
            {
                resp.ErrorMessages.Add("Grupo Muscular inválido.");
                return(resp);
            }

            if (planoTreino.UsuarioId == 0)
            {
                resp.ErrorMessages.Add("Usuario não informado");
                return(resp);
            }

            string gruposMuscularesJson = "";

            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List <GrupoMuscular>));
                    ser.WriteObject(ms, planoTreino.GruposMusculares);
                    byte[] json = ms.ToArray();
                    ms.Close();
                    gruposMuscularesJson = Encoding.UTF8.GetString(json, 0, json.Length);
                }
            }
            catch (Exception e)
            {
                _log.LogError(e.Message);
                resp.ErrorMessages.Add(e.Message);
            }

            resp.Return = planoTreino;
            var    attributeValues = new Dictionary <string, AttributeValue>();
            var    attributeNames  = new Dictionary <string, string>();
            string updExpr         = String.Empty;

            planoTreino.DtAtualizacao = DateTime.Now;
            attributeNames.Add("#dtAt", "dt-atualizacao");
            attributeValues.Add(":dtAt", new AttributeValue {
                S = planoTreino.DtAtualizacao.ToString()
            });
            updExpr = "#dtAt = :dtAt";

            if (planoTreino.Id < 1)
            {
                planoTreino.Id = (Int32)DateTimeOffset.UtcNow.ToUnixTimeSeconds();

                attributeNames.Add("#usrId", "usuario-id");
                attributeValues.Add(":usrId", new AttributeValue {
                    N = planoTreino.UsuarioId.ToString()
                });
                updExpr += ", #usrId = :usrId";
            }

            if (!String.IsNullOrEmpty(planoTreino.Descricao))
            {
                attributeNames.Add("#descr", "descricao");
                attributeValues.Add(":descr", new AttributeValue {
                    S = planoTreino.Descricao
                });
                updExpr += ", #descr = :descr";
            }

            if (!String.IsNullOrEmpty(planoTreino.Observacao))
            {
                attributeNames.Add("#obs", "observacao");
                attributeValues.Add(":obs", new AttributeValue {
                    S = planoTreino.Observacao
                });
                updExpr += ", #obs = :obs";
            }

            if (!String.IsNullOrEmpty(gruposMuscularesJson))
            {
                attributeNames.Add("#grp", "grupos-musculares");
                attributeValues.Add(":grp", new AttributeValue {
                    S = gruposMuscularesJson
                });
                updExpr += ", #grp = :grp";
            }

            var request = new UpdateItemRequest
            {
                TableName = _context.TableName,
                Key       = new Dictionary <string, AttributeValue>
                {
                    { "tipo", new AttributeValue {
                          S = TipoPlanoTreino(planoTreino)
                      } },
                    { "id", new AttributeValue {
                          N = planoTreino.Id.ToString()
                      } }
                },
                ExpressionAttributeNames  = attributeNames,
                ExpressionAttributeValues = attributeValues,
                UpdateExpression          = "SET " + updExpr
            };

            UpdateItemResponse updResponse = null;

            using (var client = _context.GetClientInstance())
            {
                try
                {
                    updResponse = await client.UpdateItemAsync(request);

                    resp.Messages.Add(updResponse.HttpStatusCode.ToString());
                }
                catch (Exception e)
                {
                    _log.LogError(e.Message);
                    resp.ErrorMessages.Add(e.Message);
                }
                return(resp);
            }
        }
예제 #2
0
        public async Task <Response <PostagemHome> > SalvarAsync(PostagemHome postagemHome)
        {
            var resp = new Response <PostagemHome>();

            if (postagemHome == null)
            {
                resp.ErrorMessages.Add("postagem nula.");
                return(resp);
            }

            if (postagemHome.UsuarioId == 0)
            {
                resp.ErrorMessages.Add("Usuario não informado");
                return(resp);
            }

            resp.Return = postagemHome;
            var    attributeValues = new Dictionary <string, AttributeValue>();
            var    attributeNames  = new Dictionary <string, string>();
            string updExpr         = String.Empty;

            postagemHome.DataAtualizacao = DateTime.Now;
            attributeNames.Add("#dtAt", "dt-atualizacao");
            attributeValues.Add(":dtAt", new AttributeValue {
                S = postagemHome.DataAtualizacao.ToString("dd/MM/yyyy hh:mm:ss")
            });
            updExpr = "#dtAt = :dtAt";

            attributeNames.Add("#ordem", "ordem");
            attributeValues.Add(":ordem", new AttributeValue {
                N = postagemHome.Ordem.ToString()
            });
            updExpr += ", #ordem = :ordem";

            if (postagemHome.Id < 1)
            {
                postagemHome.Id = (Int32)DateTimeOffset.UtcNow.ToUnixTimeSeconds();

                attributeNames.Add("#usrId", "usuario-id");
                attributeValues.Add(":usrId", new AttributeValue {
                    N = postagemHome.UsuarioId.ToString()
                });
                updExpr += ", #usrId = :usrId";
            }

            if (!String.IsNullOrEmpty(postagemHome.Titulo))
            {
                attributeNames.Add("#titulo", "titulo");
                attributeValues.Add(":titulo", new AttributeValue {
                    S = postagemHome.Titulo
                });
                updExpr += ", #titulo = :titulo";
            }

            if (!String.IsNullOrEmpty(postagemHome.Texto))
            {
                attributeNames.Add("#texto", "texto");
                attributeValues.Add(":texto", new AttributeValue {
                    S = postagemHome.Texto
                });
                updExpr += ", #texto = :texto";
            }

            attributeNames.Add("#urlImagem", "url-imagem");

            if (!String.IsNullOrEmpty(postagemHome.UrlImagem))
            {
                attributeValues.Add(":urlImagem", new AttributeValue {
                    S = postagemHome.UrlImagem
                });
                updExpr += ", #urlImagem = :urlImagem";
            }
            else
            {
                updExpr += " REMOVE #urlImagem";
            }

            var request = new UpdateItemRequest
            {
                TableName = _context.TableName,
                Key       = new Dictionary <string, AttributeValue>
                {
                    { "tipo", new AttributeValue {
                          S = _type
                      } },
                    { "id", new AttributeValue {
                          N = postagemHome.Id.ToString()
                      } }
                },
                ExpressionAttributeNames  = attributeNames,
                ExpressionAttributeValues = attributeValues,
                UpdateExpression          = "SET " + updExpr
            };

            UpdateItemResponse updResponse = null;

            using (var client = _context.GetClientInstance())
            {
                try
                {
                    updResponse = await client.UpdateItemAsync(request);

                    resp.Messages.Add(updResponse.HttpStatusCode.ToString());
                }
                catch (Exception e)
                {
                    _log.LogError(e.Message);
                    resp.ErrorMessages.Add(e.Message);
                }
                return(resp);
            }
        }
예제 #3
0
        public async Task <Response <AvaliacaoFisica> > SalvarAsync(AvaliacaoFisica avaliacaoFisica)
        {
            var resp = new Response <AvaliacaoFisica>();

            if (avaliacaoFisica == null)
            {
                resp.ErrorMessages.Add("Grupo Muscular inválido.");
                return(resp);
            }

            if (avaliacaoFisica.UsuarioId == 0)
            {
                resp.ErrorMessages.Add("Usuario não informado");
                return(resp);
            }

            resp.Return = avaliacaoFisica;
            var    attributeValues = new Dictionary <string, AttributeValue>();
            var    attributeNames  = new Dictionary <string, string>();
            string updExpr         = String.Empty;

            avaliacaoFisica.DtAtualizacao = DateTime.Now;
            attributeNames.Add("#dtAt", "dt-atualizacao");
            attributeValues.Add(":dtAt", new AttributeValue {
                S = avaliacaoFisica.DtAtualizacao.ToString("dd/MM/yyyy hh:mm:ss")
            });
            updExpr = "#dtAt = :dtAt";

            if (avaliacaoFisica.Id < 1)
            {
                avaliacaoFisica.Id = (Int32)DateTimeOffset.UtcNow.ToUnixTimeSeconds();

                attributeNames.Add("#usrId", "usuario-id");
                attributeValues.Add(":usrId", new AttributeValue {
                    N = avaliacaoFisica.UsuarioId.ToString()
                });
                updExpr += ", #usrId = :usrId";
            }

            if (!String.IsNullOrEmpty(avaliacaoFisica.Observacao))
            {
                attributeNames.Add("#obs", "observacao");
                attributeValues.Add(":obs", new AttributeValue {
                    S = avaliacaoFisica.Observacao
                });
                updExpr += ", #obs = :obs";
            }

            string avaliacaoFisicaJson = ConvertToJson(avaliacaoFisica, resp);

            if (!String.IsNullOrEmpty(avaliacaoFisicaJson))
            {
                attributeNames.Add("#avl", "avaliacao-fisica");
                attributeValues.Add(":avl", new AttributeValue {
                    S = avaliacaoFisicaJson
                });
                updExpr += ", #avl = :avl";
            }

            var request = new UpdateItemRequest
            {
                TableName = _context.TableName,
                Key       = new Dictionary <string, AttributeValue>
                {
                    { "tipo", new AttributeValue {
                          S = _type
                      } },
                    { "id", new AttributeValue {
                          N = avaliacaoFisica.Id.ToString()
                      } }
                },
                ExpressionAttributeNames  = attributeNames,
                ExpressionAttributeValues = attributeValues,
                UpdateExpression          = "SET " + updExpr
            };

            UpdateItemResponse updResponse = null;

            using (var client = _context.GetClientInstance())
            {
                try
                {
                    updResponse = await client.UpdateItemAsync(request);

                    resp.Messages.Add(updResponse.HttpStatusCode.ToString());
                }
                catch (Exception e)
                {
                    _log.LogError(e.Message);
                    resp.ErrorMessages.Add(e.Message);
                }
                return(resp);
            }
        }