public AuthenticateResponse Authenticate(AuthenticateRequest model, string ipAddress) { var account = _context.Accounts.SingleOrDefault(x => x.Email == model.Email); if (account == null || !account.IsVerified || !BC.Verify(model.Password, account.PasswordHash)) { throw new AppException("Email or password is incorrect"); } // authentication successful so generate jwt and refresh tokens string jwtToken = generateJwtToken(account); var refreshToken = generateRefreshToken(ipAddress); account.RefreshTokens.Add(refreshToken); // remove old refresh tokens from account removeOldRefreshTokens(account); // save changes to db _context.Update(account); _context.SaveChanges(); var response = _mapper.Map <AuthenticateResponse>(account); response.JwtToken = jwtToken; response.RefreshToken = refreshToken.Token; return(response); }
public Resultado Incluir(Turma dadosTurma) { Resultado resultado = DadosValidos(dadosTurma); resultado.Acao = "Inclusão de Turma"; try { if (resultado.Mensagens.Count == 0 && _context.Turmas.Where( p => p.Id != dadosTurma.Id && p.Nome == dadosTurma.Nome).Count() > 0) { resultado.Mensagens.Add( "Turma já cadastrada"); } if (resultado.Mensagens.Count == 0) { _context.Turmas.Add(dadosTurma); _context.SaveChanges(); } return(resultado); } catch (Exception ex) { resultado.Mensagens.Add("Erro:" + ex.Message); return(resultado); } }
public IHttpActionResult PutConfiguration(int id, Config configuration) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != configuration.ConfigID) { return(BadRequest()); } db.Entry(configuration).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ConfigurationExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public IHttpActionResult PutProduct(int id, Product product) { product.TaxValue = product.NetPrice * product.TaxRate * 0.01M; product.GrossPrice = product.NetPrice + product.TaxValue; if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != product.ProductId) { return(BadRequest()); } db.Entry(product).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public IHttpActionResult PutProductQuantity(int id, ProductQuantity productQuantity) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != productQuantity.ProductQuantityId) { return(BadRequest()); } db.Entry(productQuantity).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ProductQuantityExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public IHttpActionResult PutInvoice(int id, Invoice invoice) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != invoice.InvoiceId) { return(BadRequest()); } db.Entry(invoice).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!InvoiceExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult Post([FromBody] PersonasFisicas Persona) { try { context.PersonasFisicas.Add(Persona); context.SaveChanges(); return(Ok()); } catch (Exception ex) { return(BadRequest()); } }
public IHttpActionResult TaoGV(TaoGV model) { IHttpActionResult httpActionResult; ErrorModel error = new ErrorModel(); if (string.IsNullOrEmpty(model.MaGV)) { error.Add("Mã giáo viên không được để trống"); } if (string.IsNullOrEmpty(model.TenGV)) { error.Add("Họ tên giáo viên không được để trống"); } if (error.Errors.Count == 0) { Teacher teacher = new Teacher(); teacher.MaGV = model.MaGV; teacher.TenGV = model.TenGV; teacher = _db.GiaoVien.Add(teacher); _db.SaveChanges(); TeacherModel vienModel = new TeacherModel(teacher); httpActionResult = Ok(vienModel); } else { httpActionResult = Ok(error); } return(httpActionResult); }
public IHttpActionResult CreateTeacher(CreateTeacherModel model) { IHttpActionResult httpActionResult; ErrorModel error = new ErrorModel(); if (string.IsNullOrEmpty(model.TeacherId)) { error.Add("Mã giáo viên không được để trống"); } if (string.IsNullOrEmpty(model.TeacherName)) { error.Add("Họ tên giáo viên không được để trống"); } if (error.Errors.Count == 0) { Teacher teacher = new Teacher(); teacher.TeacherId = model.TeacherId; teacher.TeacherName = model.TeacherName; teacher.TeacherAddress = model.TeacherAddress; teacher = _db.Teacher.Add(teacher); _db.SaveChanges(); TeacherModel vienModel = new TeacherModel(teacher); httpActionResult = Ok(vienModel); } else { httpActionResult = new ErrorActionResult(Request, System.Net.HttpStatusCode.BadRequest, error); } return(httpActionResult); }
//Agregar un producto a la BD public bool AgregarProducto(Producto producto) { try { apiDBContext.Producto.Add(producto); apiDBContext.SaveChanges(); return(true); } catch (Exception e) { return(false); } }
//Agregar entrada public bool AgregarEntrada(Entrada entrada) { try { //Busco en la tabla stock si el producto de la entrada actual ya existe. var entradaStockDB = apiDBContext.Stock.Where(x => x.ProductoID == entrada.ProductoID).FirstOrDefault(); //Busco el registro del proveedor que quieren introducir en entrada. var proveedor = apiDBContext.Proveedor.Where(x => x.ProveedorID == entrada.ProveedorID).FirstOrDefault(); /* Sino encuentra nada, añade el objeto/modelo a la tabla entrada, crea un objeto stock, * toma los datos del objeto entrada y tambien añade el objeto stock a la tabla Stock. */ if (entradaStockDB == null) { apiDBContext.Entrada.Add(entrada); apiDBContext.Stock.Add(new Stock { ProductoID = entrada.ProductoID, Cantidad = entrada.Cantidad, Proveedores = proveedor.Nombre, Fecha = entrada.Fecha }); apiDBContext.SaveChanges(); return(true); } else { apiDBContext.Entrada.Add(entrada); /* En caso de encontrar un registro que ya contenga el mismo producto del objeto entrada, entonces, * se actualizan las propiedades de ese registro actual, con los nuevo datos de la entrada. */ entradaStockDB.Cantidad += entrada.Cantidad; /* Compruebo si ese registro ya contiene el nombre del proveedor antes de actualizar */ if (entradaStockDB.Proveedores.Contains(proveedor.Nombre)) { } else { entradaStockDB.Proveedores = entradaStockDB.Proveedores + ", " + proveedor.Nombre; } entradaStockDB.Fecha = entrada.Fecha; apiDBContext.SaveChanges(); return(true); } } catch (Exception e) { return(false); } }
public bool SaveChanges() { return(_context.SaveChanges() >= 0); }
public IActionResult AddBill(Bill bill) { _db.Bills.Add(bill); _db.SaveChanges(); return(CreatedAtRoute("GetBill", new { id = bill.BillID }, bill)); }