public void Add(Agendamentos agendamentos) { _contexto.Agendamentos.Add(agendamentos); _contexto.SaveChanges(); }
public int SalvarAgendamento(Agendamento agendamento) { if (!ValidaAgendamento(agendamento)) { return(0); } var model = this.dbContext.Agendamentos.Where(a => a.Cd_Agendamento == agendamento.Cd_Agendamento).Include(i => i.Itens).FirstOrDefault(); if (agendamento.Cd_Agendamento == 0) { var codAgendamento = this.dbContext.Agendamentos.Count() == 0 ? 1 : this.dbContext.Agendamentos.Max(a => a.Cd_Agendamento) + 1; var CodAgendamentoItem = this.dbContext.AgendamentoItens.Count() == 0 ? 1 : this.dbContext.AgendamentoItens.Max(a => a.Cd_AgendamentoItem) + 1; for (var i = 0; i < agendamento.Itens.Count; i++) { agendamento.Itens[i].Cd_AgendamentoItem = CodAgendamentoItem++; agendamento.Itens[i].Cd_Agendamento = codAgendamento; } agendamento.Cd_Agendamento = codAgendamento; this.dbContext.Agendamentos.Add(agendamento); model = agendamento; } else { model.Cd_Status = agendamento.Cd_Status; model.Dat_Agendamento = agendamento.Dat_Agendamento; model.Itens .RemoveAll((i) => { return(agendamento.Itens.Where(a => a.Cd_AgendamentoItem == i.Cd_AgendamentoItem).FirstOrDefault() == null); }); agendamento.Itens.ForEach((e) => { if (e.Cd_AgendamentoItem == 0) { var novo = new AgendamentoItem() { Cd_Agendamento = agendamento.Cd_Agendamento, Cd_Servico = e.Cd_Servico, Dat_Inicio = e.Dat_Inicio, Dat_Termino = e.Dat_Termino, Cd_Profissional = e.Cd_Profissional, Num_ValorServico = e.Num_ValorServico, Num_Comissao = e.Num_Comissao, Txt_Observacao = e.Txt_Observacao }; novo.Cd_AgendamentoItem = this.dbContext.AgendamentoItens.Count() == 0 ? 1 : this.dbContext.AgendamentoItens.Max(a => a.Cd_AgendamentoItem) + 1; dbContext.AgendamentoItens.Add(novo); } else { var item = model.Itens.Where(i => i.Cd_AgendamentoItem == e.Cd_AgendamentoItem).FirstOrDefault(); if (item == null) { dbContext.AgendamentoItens.Add(e); } else { item.Cd_Servico = e.Cd_Servico; item.Dat_Inicio = e.Dat_Inicio; item.Dat_Termino = e.Dat_Termino; item.Cd_Profissional = e.Cd_Profissional; item.Num_ValorServico = e.Num_ValorServico; item.Num_Comissao = e.Num_Comissao; item.Txt_Observacao = e.Txt_Observacao; }; } }); } dbContext.SaveChanges(); return(model.Cd_Agendamento); }