Beispiel #1
0
        public IActionResult AddEditEstado(long?id, EstadoViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    bool   isNew  = !id.HasValue;
                    Estado estado = isNew ? new Estado
                    {
                    } : context.Set <Estado>().SingleOrDefault(s => s.Id == id.Value);

                    estado.Descricao = model.Descricao;
                    estado.Pais      = model.Pais;

                    if (isNew)
                    {
                        context.Add(estado);
                    }
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(RedirectToAction("Index"));
        }
 public IActionResult AddEditBook(long?id, BookViewModel model)
 {
     try
     {
         if (ModelState.IsValid)
         {
             bool isNew = !id.HasValue;
             Book book  = isNew ? new Book
             {
                 AddedDate = DateTime.UtcNow
             } : context.Set <Book>().SingleOrDefault(s => s.Id == id.Value);
             book.Name         = model.Name;
             book.ISBN         = model.ISBN;
             book.Author       = model.Author;
             book.Publisher    = model.Publisher;
             book.IPAddress    = Request.HttpContext.Connection.RemoteIpAddress.ToString();
             book.ModifiedDate = DateTime.UtcNow;
             if (isNew)
             {
                 context.Add(book);
             }
             context.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
     return(RedirectToAction("Index"));
 }
        public IActionResult AddEditCidade(long?id, CidadeViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    bool   isNew  = !id.HasValue;
                    Cidade cidade = isNew ? new Cidade
                    {
                    } : context.Set <Cidade>().SingleOrDefault(s => s.Id == id.Value);

                    cidade.Descricao = model.Descricao;
                    cidade.Estado    = model.Estado;

                    if (isNew)
                    {
                        context.Add(cidade);
                    }
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(RedirectToAction("Index"));
        }
Beispiel #4
0
 public ActionResult AddEditCustomer(long?id, CustomerViewModel model)
 {
     try
     {
         if (ModelState.IsValid)
         {
             bool     isNew    = !id.HasValue;
             Customer customer = isNew ? new Customer
             {
                 AddedDate = DateTime.UtcNow
             } : context.Set <Customer>().SingleOrDefault(s => s.Id == id.Value);
             customer.FirstName    = model.FirstName;
             customer.LastName     = model.LastName;
             customer.MobileNo     = model.MobileNo;
             customer.Email        = model.Email;
             customer.IPAddress    = Request.HttpContext.Connection.RemoteIpAddress.ToString();
             customer.ModifiedDate = DateTime.UtcNow;
             if (isNew)
             {
                 context.Add(customer);
             }
             context.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
     return(RedirectToAction("Index"));
 }
Beispiel #5
0
 public IActionResult AddChef(Chef chef)
 {
     if (ModelState.IsValid)
     {
         var      now      = DateTime.Today;
         var      chefAge  = now - chef.Age;
         DateTime day1     = new DateTime(2018, 1, 1);
         DateTime day2     = new DateTime(2000, 1, 1);
         var      eighteen = day1 - day2;
         if (chefAge > eighteen)
         {
             dbContext.Add(chef);
             dbContext.SaveChanges();
             return(RedirectToAction("Index"));
         }
         else
         {
             ModelState.AddModelError("Age", "Age should be minimum 18");
             return(View("new"));
         }
     }
     else
     {
         return(View("new"));
     }
 }
        public IActionResult AddEditPais(long?id, PaisViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    bool isNew = !id.HasValue;
                    Pais pais  = isNew ? new Pais
                    {
                    } : context.Set <Pais>().SingleOrDefault(s => s.Id == id.Value);

                    pais.Descricao = model.Descricao;

                    if (isNew)
                    {
                        context.Add(pais);
                    }
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(RedirectToAction("Index"));
        }
Beispiel #7
0
        public IHttpActionResult PutEmployee(int id, Employee employee)
        {
            if (id != employee.EmployeeId)
            {
                return(BadRequest());
            }

            db.Entry(employee).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EmployeeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
 public object AddUpdateUsers(string userData)
 {
     try
     {
         var user = JsonConvert.DeserializeAnonymousType(userData, new Users());
         if (user != null)
         {
             var isUserExist = _context.Users.FirstOrDefault(x => x.Id == user.Id);
             if (isUserExist == null)
             {
                 _context.Users.Add(user);
                 _context.SaveChanges();
                 return(Ok(new { message = "Data added successfully", status = true }));
             }
             else
             {
                 isUserExist.Name     = user.Name;
                 isUserExist.Email    = user.Email;
                 isUserExist.Password = user.Password;
                 _context.SaveChanges();
                 return(Ok(new { message = "Data updated successfully", status = true }));
             }
         }
         else
         {
             return(Ok(new { message = "Something went wrong", status = false }));
         }
     }
     catch (Exception ex)
     {
         return(Ok(new { message = ex.Message, status = false }));
     }
 }
Beispiel #9
0
 public IActionResult AddEditPaciente(long?id, PacienteViewModel model)
 {
     try
     {
         if (ModelState.IsValid)
         {
             bool     isNew    = !id.HasValue;
             Paciente paciente = isNew ? new Paciente
             {
             } : context.Set <Paciente>().SingleOrDefault(s => s.Id == id.Value);
             paciente.Nome   = model.Nome;
             paciente.CPF    = model.CPF;
             paciente.Pais   = model.Pais;
             paciente.Estado = model.Estado;
             paciente.Cidade = model.Cidade;
             if (isNew)
             {
                 context.Add(paciente);
             }
             context.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
     return(RedirectToAction("Index"));
 }
Beispiel #10
0
 public ActionResult AddEditBooks(long?id, BookViewModel model)
 {
     try
     {
         if (ModelState.IsValid)
         {
             bool  isNew = !id.HasValue;
             Books book  = isNew ? new Books
             {
                 AddedDate = DateTime.UtcNow
             } : context.Set <Books>().SingleOrDefault(s => s.Id == id.Value);
             book.Id            = model.Id;
             book.Author        = model.Author;
             book.Title         = model.Title;
             book.YearPublished = model.Year;
             book.ModifiedDate  = DateTime.UtcNow;
             if (isNew)
             {
                 context.Add(book);
             }
             context.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
     return(RedirectToAction("Index"));
 }
Beispiel #11
0
        public JsonResult delete(int id)
        {
            Student lst = context.Students.Where(o => o.Id == id).FirstOrDefault() as Student;

            context.Students.Remove(lst);
            context.SaveChanges();
            return(Json(lst, JsonRequestBehavior.AllowGet));
        }
        public JsonResult Create([FromBody] Product Product)


        {
            if (ModelState.IsValid)
            {
                _context.Add(Product);
                _context.SaveChanges();
            }
            return(Json(Product));
        }
        public ActionResult Create([Bind(Include = "ID,Nome")] Restaurante restaurante)
        {
            if (ModelState.IsValid)
            {
                db.Restaurante.Add(restaurante);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(restaurante));
        }
        public JsonResult Create([FromBody] Customer Customer)


        {
            _context.Add(Customer);
            _context.SaveChanges();
            // return RedirectToAction(nameof(Index));


            return(Json(Customer));
        }
Beispiel #15
0
        public JsonResult Create([FromBody] Sale Sale)


        {
            if (ModelState.IsValid)
            {
                _context.Add(Sale);
                _context.SaveChanges();
            }
            return(Json(Sale));
        }
        public ActionResult Create([Bind(Include = "ID,Nome,Preco,RestauranteID")] Prato prato)
        {
            if (ModelState.IsValid)
            {
                db.Prato.Add(prato);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.RestauranteID = new SelectList(db.Restaurante, "ID", "Nome", prato.RestauranteID);
            return(View(prato));
        }
Beispiel #17
0
        public JsonResult Create([FromBody] Sale Sale)


        {
            if (ModelState.IsValid)
            {
                _context.Add(Sale);
                _context.SaveChanges();
                // return RedirectToAction(nameof(Index));
            }
            return(Json(Sale));
        }
        public ActionResult Create([Bind(Include = "SolicitacaoCompraId,DataSolicitacao,UsuarioSolicitante,DeptoSolicitante,Quantidade,DataLimiteRecebimento,StatusSolicitacao,ProdutoId")] SolicitacaoCompra solicitacaoCompra)
        {
            if (ModelState.IsValid)
            {
                db.SolicitacaoCompra.Add(solicitacaoCompra);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.ProdutoId = new SelectList(db.Produto, "ProdutoId", "NomeProduto", solicitacaoCompra.ProdutoId);
            return(View(solicitacaoCompra));
        }
Beispiel #19
0
 bool IClienteRepositorio.Alterar(Cliente cliente)
 {
     try
     {
         _db.Entry(cliente).State = EntityState.Modified;
         _db.SaveChanges();
         return(true);
     }
     catch (Exception ex) // gravar em log
     {
         return(false);
     }
 }
Beispiel #20
0
 bool IPedidoRepositorio.Remover(Pedido pedido)
 {
     try
     {
         _db.Remove(pedido);
         _db.SaveChanges();
         return(true);
     }
     catch (Exception ex) // gravar em log
     {
         return(false);
     }
 }
 public IActionResult Create(Dish newDish)
 {
     if (ModelState.IsValid)
     {
         dbContext.Add(newDish);
         dbContext.SaveChanges();
         return(RedirectToAction("Index"));
     }
     else
     {
         return(View("New"));
     }
 }
Beispiel #22
0
        public void GetComments()
        {
            var request = new RestRequest("users");

            IRestResponse response = client.Execute(request);

            users = JsonConvert.DeserializeObject <List <User> >(response.Content);


            var requestPost = new RestRequest("posts");

            IRestResponse responsePost = client.Execute(requestPost);

            posts = JsonConvert.DeserializeObject <List <Post> >(responsePost.Content);

            var requestComments = new RestRequest("comments");

            IRestResponse responseComments = client.Execute(requestComments);

            comments = JsonConvert.DeserializeObject <List <Comment> >(responseComments.Content);

            _context.Users.AddRange(users);
            _context.Posts.AddRange(posts);
            _context.Comments.AddRange(comments);
            _context.SaveChanges();
        }
        public ActionResult Add(Product _product)
        {
            if (ModelState.IsValid)
            {
                Product product = new Product()
                {
                    Name        = _product.Name,
                    Description = _product.Description,
                    Price       = _product.Price
                };

                _db.Products.Add(product);
                _db.SaveChanges();

                return(RedirectToAction("Main"));
            }

            return(new HttpStatusCodeResult(400));
        }
Beispiel #24
0
 public IActionResult Create(Meal dish)
 {
     if (ModelState.IsValid)
     {
         Meal newDish = new Meal
         {
             Name        = dish.Name,
             Chef        = dish.Chef,
             Tastiness   = dish.Tastiness,
             Calories    = dish.Calories,
             Description = dish.Description
         };
         dbContext.Add(newDish);
         dbContext.SaveChanges();
         return(RedirectToAction("Index"));
     }
     else
     {
         return(View("New"));
     }
 }
Beispiel #25
0
 public IActionResult CreateDish(Dish NewDish)
 {
     if (ModelState.IsValid)
     {
         Dish newDish = new Dish()
         {
             Name        = NewDish.Name,
             Chef        = NewDish.Chef,
             Tastiness   = NewDish.Tastiness,
             Calories    = NewDish.Calories,
             Description = NewDish.Description,
         };
         dbContext.Add(newDish);
         dbContext.SaveChanges();
         return(Redirect("/"));
     }
     else
     {
         return(View("New"));
     }
 }
 public ActionResult Create(AutorViewModel model)
 {
     try
     {
         var autor = Mapper.Map <AutorViewModel, Autor>(model);
         if (autor == null)
         {
             return(HttpNotFound());
         }
         if (true)
         {
             db.Autores.Add(autor);
             db.SaveChanges();
         }
         return(RedirectToAction("Index"));
     }
     catch
     {
         return(View());
     }
 }
Beispiel #27
0
        public JsonResult Delete(int Id)
        {
            var rec = from stu in context.Students
                      join cl in context.Classes on stu.Class_Id equals cl.Id
                      where stu.Class_Id == Id
                      select new { stu, cl };

            foreach (var i in rec)
            {
                context.Students.Remove(i.stu);
                //context.Classes.Remove(i.cl);
            }
            context.SaveChanges();
            return(Json(rec, JsonRequestBehavior.AllowGet));
        }
        public RequestObject SignIn(LoginUI login)
        {
            var request      = new RequestObject();
            var isUserExists = CRUDContext.UserLoginInfo.AsNoTracking()
                               .Where(u => u.EmailId == login.EmailId).Any();

            if (!isUserExists)
            {
                using (var scope = CRUDContext.Database.BeginTransaction())
                {
                    try
                    {
                        var userLogin = new UserLoginInfo()
                        {
                            FirstName   = login.FirstName,
                            LastName    = login.LastName,
                            EmailId     = login.EmailId,
                            Password    = login.Password,
                            LogInTime   = DateTime.Now,
                            CreatedBy   = login.UserName,
                            CreatedDate = DateTime.Now
                        };
                        CRUDContext.UserLoginInfo.Add(userLogin);
                        CRUDContext.SaveChanges();
                        request.status = true;
                        scope.Commit();
                    }
                    catch (DbUpdateException ex)
                    {
                        request.IsDbException = true;
                        request.ErrorMessage  = ex.Message;
                        scope.Rollback();
                    }
                    catch (Exception Ex)
                    {
                        request.status       = false;
                        request.ErrorMessage = Ex.Message;
                        scope.Rollback();
                    }
                }
            }
            else
            {
                request.status = isUserExists;
            }

            return(request);
        }
Beispiel #29
0
        public IActionResult createAccount(User newUser)
        {
            if (ModelState.IsValid)
            {
                if (dbContext.users.Any(u => u.Email == newUser.Email))
                {
                    return(View("LoginReg"));
                    // You may consider returning to the View at this point
                }
                PasswordHasher <User> Hasher = new PasswordHasher <User>();
                newUser.Password = Hasher.HashPassword(newUser, newUser.Password);
                dbContext.Add(newUser);
                dbContext.SaveChanges();
                HttpContext.Session.SetString("UserName", newUser.Fname);

                return(RedirectToAction("Success"));
            }
            else
            {
                return(View("LoginReg"));
            }
        }
Beispiel #30
0
        public ActionResult Register(StudentModel studentModel)
        {
            try
            {
                CRUDContext db = new CRUDContext();
                if (studentModel.Id > 0)
                {
                    Student student = db.Students.Include(x => x.Address).Where(x => x.Id == studentModel.Id).FirstOrDefault();
                    student.Email = studentModel.Email;
                    student.Name  = studentModel.Name;



                    db.Entry(student).State = EntityState.Modified;
                }
                else
                {
                    db.Students.Add(StudentMapper.ModelToContext(studentModel));
                }
                db.SaveChanges();
                var students = new CRUDContext().Students.Include(x => x.Address).ToList();
                List <StudentModel> studentModels = new List <StudentModel>();
                foreach (Student s in students)
                {
                    studentModels.Add(StudentMapper.ContextToModel(s));
                }

                return(Json(studentModels, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                return(Json(e.Message, JsonRequestBehavior.AllowGet));
            }

            return(Json(false, JsonRequestBehavior.AllowGet));
        }