Example #1
0
        public void OnActionExecuting(ActionExecutingContext context)
        {
            try
            {
                using (db = new AxosnetAPIContext())
                {
                    int?id = null;

                    if (context.ActionArguments.ContainsKey("id"))
                    {
                        id = Convert.ToInt32(context.ActionArguments["id"]);
                    }
                    else
                    {
                        context.Result = new BadRequestObjectResult(new { message = "Bad id parameter" });
                        return;
                    }

                    var entity = db.Set <T>().Find(id);
                    if (entity == null)
                    {
                        context.Result = new NotFoundResult();
                    }
                }
            }
            catch (Exception)
            {
                context.Result = new StatusCodeResult(500);
            }
        }
Example #2
0
        public UserViewModel GeUserSession(string token)
        {
            try
            {
                token = token.Substring(7); // BEARER
                JwtSecurityTokenHandler handler   = new JwtSecurityTokenHandler();
                JwtSecurityToken        jsonToken = handler.ReadToken(token) as JwtSecurityToken;

                string idUserString = jsonToken.Claims.FirstOrDefault(claim => claim.Type == "nameid").Value;
                int    idUser       = 0;
                bool   validId      = int.TryParse(idUserString, out idUser);


                if (validId)
                {
                    using (db = new AxosnetAPIContext())
                    {
                        User userSession = db.Users.FirstOrDefault(u => u.IdUser == idUser);
                        return((userSession != null) ? ConvertUser(userSession) : throw new Exception("Sign in please"));
                    }
                }

                throw new Exception("Sign in please");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #3
0
        public ActionResult GetById(int?id)
        {
            try
            {
                string token = Request.Headers["Authorization"].First();
                User   user  = authLogic.GetUserByToken(token);

                using (db = new AxosnetAPIContext())
                {
                    Receipt receipt = db.Receipts.
                                      Include(receipt => receipt.Currency).
                                      FirstOrDefault(receipt => receipt.IdReceipt == id.Value);

                    if (receipt.IdUser != user.IdUser)
                    {
                        return(Unauthorized(new { errorMessage = "You can't see this" }));
                    }

                    return(Ok(receipt));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex));
            }
        }
Example #4
0
 public ActionResult GetAll()
 {
     try
     {
         using (db = new AxosnetAPIContext())
         {
             List <Currency> currencies = db.Currencies.ToList();
             return(Ok(currencies));
         }
     }
     catch (Exception ex)
     {
         return(StatusCode(500, ex));
     }
 }
Example #5
0
 // return true if not exists in database
 public bool validateUserNotExists(string Email)
 {
     try
     {
         using (db = new AxosnetAPIContext())
         {
             User user = db.Users.FirstOrDefault(u => u.Email == Email);
             return(user == null);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Example #6
0
        public ActionResult GetById(int?id)
        {
            try
            {
                using (db = new AxosnetAPIContext())
                {
                    Currency receipt = db.Currencies.
                                       FirstOrDefault(currency => currency.IdCurrency == id);

                    return(Ok(receipt));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex));
            }
        }
Example #7
0
        public ActionResult Delete(int?id)
        {
            try
            {
                using (db = new AxosnetAPIContext())
                {
                    Receipt receipt = db.Receipts.Find(id);
                    db.Receipts.Remove(receipt);
                    db.SaveChanges();

                    return(Ok());
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex));
            }
        }
Example #8
0
 public ActionResult GetAll()
 {
     try
     {
         string token = Request.Headers["Authorization"].First();
         User   user  = authLogic.GetUserByToken(token);
         using (db = new AxosnetAPIContext())
         {
             List <Receipt> receipts = db.Receipts
                                       .Include(receipt => receipt.Currency)
                                       .Where(r => r.IdUser == user.IdUser).ToList();
             return(Ok(receipts));
         }
     }
     catch (Exception ex)
     {
         return(StatusCode(500, ex));
     }
 }
Example #9
0
        public ActionResult Create([Bind("ProviderCode,Amount,Date,Comments,IdCurrency")]
                                   [FromBody] Receipt receipt)
        {
            try
            {
                string token = Request.Headers["Authorization"].First();
                User   user  = authLogic.GetUserByToken(token);
                receipt.IdUser = user.IdUser;
                using (db = new AxosnetAPIContext())
                {
                    db.Receipts.Add(receipt);
                    db.SaveChanges();

                    return(Ok(receipt));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex));
            }
        }
Example #10
0
        public ActionResult Edit(int?id, [Bind("IdReceipt,ProviderCode,Amount,Date,Comments,IdCurrency")]
                                 [FromBody] Receipt receipt)
        {
            try
            {
                if (receipt.IdUser == null)
                {
                    return(Unauthorized(new { errorMessage = "You can't do that" }));
                }
                using (db = new AxosnetAPIContext())
                {
                    db.Update(receipt);
                    db.SaveChanges();

                    return(Ok(receipt));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex));
            }
        }
Example #11
0
        public ActionResult CreateUser([FromBody] User user)
        {
            try
            {
                if (authLogic.validateUserNotExists(user.Email))
                {
                    user.Password = authLogic.Encrypt(user.Password);
                    using (db = new AxosnetAPIContext())
                    {
                        db.Users.Add(user);
                        db.SaveChanges();

                        return(Ok());
                    }
                }

                return(BadRequest(new { errorMessage = "The user already exists" }));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex));
            }
        }
Example #12
0
        public User loginValidate(LoginViewModel login)
        {
            try
            {
                using (db = new AxosnetAPIContext())
                {
                    User user = db.Users.FirstOrDefault(u => u.Email == login.email);

                    if (user != null)
                    {
                        if (Decrypt(user.Password) == login.password)
                        {
                            return(user);
                        }
                    }
                }

                return(null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }