private string TipoPlanoTreino(PlanoTreino planoTreino) { string tpPlanoTreino = "plano-treino"; if ((planoTreino.UsuarioId == planoTreino.usuario.Id) && (planoTreino.usuario.Administrador)) { tpPlanoTreino = "plano-treino-template"; } return(tpPlanoTreino); }
public async Task <IActionResult> PostAsync([FromBody] PlanoTreino planoTreino) { var usr = this.ObterUsuario(); planoTreino.usuario = usr; var response = await _planoTreinoRepository.SalvarAsync(planoTreino); if (response.Return == null) { return(NotFound()); } if (response.HasError) { return(BadRequest(response.ErrorMessages)); } return(CreatedAtRoute(routeName: "ObterPlanoTreino", routeValues: new { userId = planoTreino.UsuarioId }, value: response.Return)); }
public async Task <IActionResult> DeleteAsync(int planoTreinoId) { if (planoTreinoId < 0) { return(BadRequest("identificador inválido.")); } var usr = this.ObterUsuario(); var planoTreino = new PlanoTreino(); planoTreino.Id = planoTreinoId; planoTreino.usuario = usr; planoTreino.UsuarioId = usr.Id; var response = await _planoTreinoRepository.ExcluirAsync(planoTreino); if (response.HasError) { return(BadRequest(response.ErrorMessages)); } return(Ok()); }
public async Task <Response <bool> > ExcluirAsync(PlanoTreino planoTreino) { using (var client = _context.GetClientInstance()) { DeleteItemRequest request = new DeleteItemRequest { TableName = _context.TableName, Key = new Dictionary <string, AttributeValue> { { "tipo", new AttributeValue { S = TipoPlanoTreino(planoTreino) } }, { "id", new AttributeValue { N = planoTreino.Id.ToString() } } }, }; Response <bool> resp = new Response <bool>(); try { var delResponse = await client.DeleteItemAsync(request); resp.Messages.Add(delResponse.HttpStatusCode.ToString()); resp.Return = true; } catch (Exception e) { resp.Return = false; _log.LogError(e.Message); resp.ErrorMessages.Add(e.Message); } return(resp); } }
private List <PlanoTreino> ExtractPlanoTreino <T>(Response <T> resp, QueryResponse response) { List <PlanoTreino> lstPlanoTreino = new List <PlanoTreino>(); foreach (Dictionary <string, AttributeValue> item in response.Items) { var planoTreino = new PlanoTreino(); foreach (KeyValuePair <string, AttributeValue> kvp in item) { string attributeName = kvp.Key; AttributeValue value = kvp.Value; if (attributeName == "id") { planoTreino.Id = int.Parse(value.N); } else if (attributeName == "observacao") { planoTreino.Observacao = value.S; } else if (attributeName == "descricao") { planoTreino.Descricao = value.S; } else if (attributeName == "dt-atualizacao") { DateTime dtAtual; DateTime.TryParse(value.S, out dtAtual); planoTreino.DtAtualizacao = dtAtual; } else if (attributeName == "usuario-id") { planoTreino.UsuarioId = int.Parse(value.N); } else if (attributeName == "grupos-musculares") { if (!String.IsNullOrEmpty(value.S)) { try { using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(value.S))) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List <GrupoMuscular>)); var list = (List <GrupoMuscular>)ser.ReadObject(ms); ms.Close(); planoTreino.GruposMusculares = list; } } catch (Exception e) { _log.LogError(e.Message); resp.ErrorMessages.Add(e.Message); } } } } lstPlanoTreino.Add(planoTreino); } return(lstPlanoTreino); }
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); } }