Exemplo n.º 1
0
        public JsonResult GetCustomer()
        {
            string          sql    = "SELECT * FROM customer";
            MySqlDataReader reader = new Database().Query(sql);

            ResultListModel <CustomerModel> result         = new ResultListModel <CustomerModel>();
            List <CustomerModel>            customers      = new List <CustomerModel>();
            ResponseStatusModel             responseStatus = new ResponseStatusModel()
            {
                StatusCode = StatusCode.SUCCESS, ClientMsg = "SUCCESS", MessageType = MessageType.NONE, Operation = Operation.GetCustomer
            };

            while (reader.Read())
            {
                CustomerModel customer = new CustomerModel();
                customer.Id        = reader.GetInt16("Id");
                customer.Firstname = reader.GetString("FirstName");
                customer.Lastname  = reader.GetString("LastName");
                customer.Username  = reader.GetString("UserName");
                customer.Password  = reader.GetString("Password");
                customer.Role      = reader.GetInt16("Role");
                customer.Token     = reader.GetString("Token");
                customers.Add(customer);
            }
            result.result         = customers;
            result.responseStatus = responseStatus;
            return(Json(result, JsonRequestBehavior.AllowGet));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> SignIn([FromBody] SignInRequestModel model)
        {
            var result = await this.signInManager.PasswordSignInAsync(model.Login, model.Password, false, false);

            if (result.Succeeded)
            {
                User user = await this.userManager.FindByNameAsync(model.Login);

                UserDto userDto = new UserDto
                {
                    ID      = user.Id,
                    Name    = user.UserName,
                    Email   = user.Email,
                    Country = user.Country,
                    Phone   = user.PhoneNumber,
                    Year    = user.Year
                };

                HttpContext.Session.Remove("guestID");
                HttpContext.Session.Set <UserDto>("current-user", userDto);

                this.responseStatus = new ResponseStatusModel {
                    Success = result.Succeeded
                };
                return(Ok(new { ResponseStatus = this.responseStatus }));
            }
            else
            {
                this.responseStatus = new ResponseStatusModel {
                    Success = result.Succeeded, Message = "Invalid login or password", Code = 400
                };
                return(BadRequest(new { ResponseStatus = this.responseStatus }));
            }
        }
Exemplo n.º 3
0
        public async Task <IActionResult> SignIn([FromBody] SignInRequestModel model)
        {
            User user = await this.userManager.FindByLoginAsync(model.Login);

            if (user != null)
            {
                if (user.Roles.Any(r => r != "admin"))
                {
                    this.responseStatus = new ResponseStatusModel {
                        Success = false, Message = "This user is not an administrator", Code = 400
                    };
                    return(BadRequest(new { ResponseStatus = this.responseStatus }));
                }

                string token = JWTHelper.GetToken(user);

                this.responseStatus = new ResponseStatusModel {
                    Success = true
                };
                return(Ok(new { ResponseStatus = this.responseStatus, Token = token }));
            }
            else
            {
                this.responseStatus = new ResponseStatusModel {
                    Success = false, Message = "Invalid login or password", Code = 400
                };
                return(BadRequest(new { ResponseStatus = this.responseStatus }));
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> UpdateProduct([FromBody] ProductRequestModel request)
        {
            try
            {
                await this.productManager.UpdateProductAsync(new Product
                {
                    _Id             = request._Id,
                    Tag             = request.Tag,
                    Type            = request.Type,
                    Characteristics = request.Characteristics
                });

                this.responseStatus = new ResponseStatusModel {
                    Success = true, Message = "Data successfully updated"
                };
                return(Ok(new { ResponseStatus = this.responseStatus }));
            }
            catch (ArgumentException ex)
            {
                this.responseStatus = new ResponseStatusModel {
                    Success = false, Message = ex.Message, Code = 400
                };
                return(BadRequest(new { ResponseStatus = this.responseStatus }));
            }
        }
Exemplo n.º 5
0
        public async Task <IActionResult> AddProduct(int id)
        {
            try
            {
                if (User.Identity.IsAuthenticated)
                {
                    await this.cartManager.AddProduct(id, HttpContext.Session.Get <UserDto>("current-user").ID);
                }
                else
                {
                    if (HttpContext.Session.Get <string>("guestID") == null)
                    {
                        string anonymID = Guid.NewGuid().ToString();
                        HttpContext.Session.Set <string>("guestID", anonymID);
                    }
                    await this.cartManager.AddProduct(id, HttpContext.Session.Get <string>("guestID"));
                }

                this.responseStatus = new ResponseStatusModel {
                    Success = true, Message = "Product successfully added to the Shopping Cart"
                };
                return(Ok(new { ResponseStatus = responseStatus }));
            }
            catch (ArgumentException ex)
            {
                this.responseStatus = new ResponseStatusModel {
                    Code = 404, Message = ex.Message, Success = false
                };
                return(NotFound(new { ResponseStatus = this.responseStatus }));
            }
        }
Exemplo n.º 6
0
        public async Task <IActionResult> Checkout([FromBody] CheckoutRequestModel request)
        {
            string guestID = HttpContext.Session.Get <string>("guestID");
            string userID  = (HttpContext.Session.Get <UserDto>("current-user"))?.ID;

            try
            {
                await this.cartManager.Checkout(new OrderDto
                {
                    UserID          = userID,
                    GuestID         = guestID,
                    UserName        = request.UserName,
                    Phone           = request.Phone,
                    DeliveryAddress = request.DeliveryAddress,
                    TotalPrice      = request.TotalPrice
                });

                this.responseStatus = new ResponseStatusModel {
                    Success = true
                };
                return(Ok(new { ResponseStatus = this.responseStatus }));
            }
            catch (ArgumentException ex)
            {
                return(NotFound(new { Success = false, Error = ex.Message }));
            }
        }
        public async Task <IActionResult> GetShoppingCart()
        {
            try
            {
                string       token  = Request.Headers.FirstOrDefault(h => h.Key == "Authorization").Value;
                Guid         userID = new Guid(JWTHelper.GetClaimData(token.Remove(0, token.LastIndexOf(' ') + 1), ClaimsTypeConst.ID));
                ShoppingCart cart   = await this.cartManager.GetShoppingCartAsync(userID);

                if (cart == null)
                {
                    this.responseStatus = new ResponseStatusModel {
                        Success = true, Message = "Shopping Cart is empty.", Code = 204
                    };
                    return(NoContent());
                }

                this.responseStatus = new ResponseStatusModel {
                    Success = true
                };
                return(Ok(new { ResponseStatus = this.responseStatus, Cart = cart }));
            }
            catch (ArgumentException ex)
            {
                this.responseStatus = new ResponseStatusModel {
                    Success = true, Message = ex.Message, Code = 204
                };
                return(NoContent());
            }
        }
        public async Task <IActionResult> GetProducts(string type)
        {
            List <Product> produtcs = await this.productManager.GetProductsAsync(type);

            this.responseStatus = new ResponseStatusModel {
                Success = true
            };
            return(Ok(new { ResponseStatus = this.responseStatus, Products = produtcs }));
        }
Exemplo n.º 9
0
        public async Task <IActionResult> GetProductTypes()
        {
            List <string> types = await this.productManager.GetProductTypesAsync();

            this.responseStatus = new ResponseStatusModel {
                Success = true
            };
            return(Ok(new { ResponseStatus = this.responseStatus, Types = types }));
        }
Exemplo n.º 10
0
        public async Task <IActionResult> GetProducts()
        {
            IEnumerable <Product> produtcs = await this.productManager.GetProductsAsync();

            this.responseStatus = new ResponseStatusModel {
                Success = true
            };
            return(Ok(new { ResponseStatus = this.responseStatus, Products = produtcs }));
        }
Exemplo n.º 11
0
        public async Task <IActionResult> GetProducts(int id)
        {
            ProductDto[] produtcs = await this.productManager.GetProducts(id);

            this.responseStatus = new ResponseStatusModel {
                Success = true
            };
            return(Ok(new { ResponseStatus = this.responseStatus, Products = produtcs }));
        }
        public async Task <IActionResult> GetOrderList()
        {
            IEnumerable <Order> orders = await this.orderManager.GetOrderListAsync();

            if (orders == null || orders.Count() == 0)
            {
                return(NoContent());
            }

            this.responseStatus = new ResponseStatusModel {
                Success = true
            };
            return(Ok(new { ResponseStatus = this.responseStatus, OrderList = orders }));
        }
Exemplo n.º 13
0
        public async Task <IActionResult> GetOrderList()
        {
            OrderDto[] orders = await this.orderManager.GetOrderList();

            if (orders == null || orders.Length == 0)
            {
                return(NoContent());
            }

            this.responseStatus = new ResponseStatusModel {
                Success = true
            };
            return(Ok(new { ResponseStatus = this.responseStatus, OrderList = orders }));
        }
        public async Task <IHttpActionResult> Login(LoginModel model)
        {
            logger.Info("Controller:Accountr, Method:Login, Parameters={Username:"******", Password" + model.password + "}");
            ResponseStatusModel <BearerTokenModel> response = new ResponseStatusModel <BearerTokenModel>();

            response.data = new BearerTokenModel();
            if (ModelState.IsValid)
            {
                string tokenUrl = CommonHelper.GetSiteUrl() + "token";
                using (HttpClient httpClient = new HttpClient())
                {
                    HttpContent content = new FormUrlEncodedContent(new[]
                    {
                        new KeyValuePair <string, string>("grant_type", "password"),
                        new KeyValuePair <string, string>("username", model.userName),
                        new KeyValuePair <string, string>("password", model.password)
                    });

                    HttpResponseMessage result = await httpClient.PostAsync(tokenUrl, content);

                    string resultContent = result.Content.ReadAsStringAsync().Result;
                    var    token         = JsonConvert.DeserializeObject <BearerTokenModel>(resultContent);
                    if (!string.IsNullOrEmpty(token.access_token))
                    {
                        response.code    = Convert.ToInt32(HttpStatusCode.OK);
                        response.status  = true;
                        response.data    = token;
                        response.message = "Success";
                        return(Content(HttpStatusCode.OK, response));
                    }
                    else
                    {
                        response.code    = Convert.ToInt32(HttpStatusCode.BadRequest);
                        response.status  = false;
                        response.data    = null;
                        response.message = "Invalid username or password.";
                        return(Content(HttpStatusCode.BadRequest, response));
                    }
                }
            }
            else
            {
                response.code    = Convert.ToInt32(HttpStatusCode.BadRequest);
                response.status  = false;
                response.message = CommonHelper.GetModalErrorResult(ModelState);
                return(Content(HttpStatusCode.BadRequest, response));
            }
        }
Exemplo n.º 15
0
        public async Task <IActionResult> FindProduct(int id)
        {
            ProductDto product = await this.productManager.FindProduct(id);

            if (product != null)
            {
                this.responseStatus = new ResponseStatusModel {
                    Success = true
                };
                return(Ok(new { ResponseStatus = this.responseStatus, Product = product }));
            }

            this.responseStatus = new ResponseStatusModel {
                Success = false, Message = "No result."
            };
            return(Ok(new { ResponseStatus = this.responseStatus }));
        }
Exemplo n.º 16
0
        public async Task <IActionResult> GetOrderList()
        {
            string guestID = HttpContext.Session.Get <string>("guestID");
            string userID  = (HttpContext.Session.Get <UserDto>("current-user"))?.ID;

            OrderDto[] orders = await this.orderManager.GetOrderList(userID, guestID);

            if (orders == null || orders.Length == 0)
            {
                return(NoContent());
            }

            this.responseStatus = new ResponseStatusModel {
                Success = true
            };
            return(Ok(new { ResponseStatus = this.responseStatus, OrderList = orders }));
        }
Exemplo n.º 17
0
        public async Task <IActionResult> SearchProduct(string keyword)
        {
            ProductDto[] products = await this.productManager.SearchProducts(keyword);

            if (products != null && products.Length > 0)
            {
                this.responseStatus = new ResponseStatusModel {
                    Success = true
                };
                return(Ok(new { ResponseStatus = this.responseStatus, Products = products }));
            }

            this.responseStatus = new ResponseStatusModel {
                Success = false, Message = "No result."
            };
            return(Ok(new { ResponseStatus = this.responseStatus }));
        }
        public async Task <IActionResult> SearchProduct(string keyword)
        {
            IEnumerable <Product> products = await this.productManager.SearchProductsAsync(Guid.Empty, keyword);

            if (products != null && products.Count() > 0)
            {
                this.responseStatus = new ResponseStatusModel {
                    Success = true
                };
                return(Ok(new { ResponseStatus = this.responseStatus, Products = products }));
            }

            this.responseStatus = new ResponseStatusModel {
                Success = false, Message = "No result."
            };
            return(Ok(new { ResponseStatus = this.responseStatus }));
        }
Exemplo n.º 19
0
        public async Task <IActionResult> GetOrderList()
        {
            string token  = Request.Headers.FirstOrDefault(h => h.Key == "Authorization").Value;
            string userID = JWTHelper.GetClaimData(token.Remove(0, token.LastIndexOf(' ') + 1), ClaimsTypeConst.ID);

            IEnumerable <Order> orders = await this.orderManager.GetOrderListAsync(userID);

            if (orders == null || orders.Count() == 0)
            {
                return(NoContent());
            }

            this.responseStatus = new ResponseStatusModel {
                Success = true
            };
            return(Ok(new { ResponseStatus = this.responseStatus, OrderList = orders }));
        }
Exemplo n.º 20
0
        public async Task <IActionResult> Registration([FromBody] RegistrationRequestModel model)
        {
            if (await this.userManager.FindByNameAsync(model.Login) != null)
            {
                this.responseStatus = new ResponseStatusModel {
                    Success = false, Message = "This login already use.", Code = 409
                };
                return(StatusCode(409, new { ResponseStatus = this.responseStatus }));
            }

            if (await this.userManager.FindByEmailAsync(model.Email) != null)
            {
                this.responseStatus = new ResponseStatusModel {
                    Success = false, Message = "This email already use.", Code = 409
                };
                return(StatusCode(409, new { ResponseStatus = this.responseStatus }));
            }

            var result = await this.userManager
                         .CreateAsync(new User
            {
                UserName    = model.Login,
                Email       = model.Email,
                Country     = model.Country,
                PhoneNumber = model.Phone,
                Year        = model.Year
            }, model.Password);

            if (result.Succeeded)
            {
                User user = await this.userManager.FindByNameAsync(model.Login);

                await userManager.AddToRoleAsync(user, "user");

                this.responseStatus = new ResponseStatusModel {
                    Success = true
                };
                return(Ok(new { ResponseStatus = this.responseStatus }));
            }

            this.responseStatus = new ResponseStatusModel {
                Success = false, Code = 400
            };
            return(BadRequest(new { ResponseStatus = this.responseStatus }));
        }
Exemplo n.º 21
0
        public async Task <IActionResult> GetShoppingCart()
        {
            try
            {
                ShoppingCartDto cart;
                if (User.Identity.IsAuthenticated)
                {
                    cart = await this.cartManager.GetShoppingCartProducts(HttpContext.Session.Get <UserDto>("current-user").ID);

                    if (cart != null)
                    {
                        cart.UserName = HttpContext.Session.Get <UserDto>("current-user").Name;
                    }
                }
                else
                {
                    cart = await this.cartManager.GetShoppingCartProducts(HttpContext.Session.Get <string>("guestID"));

                    if (cart != null)
                    {
                        cart.UserName = "******";
                    }
                }
                if (cart == null)
                {
                    this.responseStatus = new ResponseStatusModel {
                        Success = true, Message = "Shopping Cart is empty.", Code = 204
                    };
                    return(NoContent());
                }

                this.responseStatus = new ResponseStatusModel {
                    Success = true
                };
                return(Ok(new { ResponseStatus = this.responseStatus, Cart = cart }));
            }
            catch (ArgumentException ex)
            {
                this.responseStatus = new ResponseStatusModel {
                    Success = true, Message = ex.Message, Code = 204
                };
                return(NoContent());
            }
        }
Exemplo n.º 22
0
        public async Task <IActionResult> DeleteItem(int id)
        {
            try
            {
                await this.cartManager.DeleteItem(id);

                this.responseStatus = new ResponseStatusModel {
                    Success = true
                };
                return(Ok(new { ResponseStatus = this.responseStatus }));
            }
            catch (ArgumentException ex)
            {
                this.responseStatus = new ResponseStatusModel {
                    Success = false, Message = ex.Message, Code = 404
                };
                return(NotFound(new { ResponseStatus = this.responseStatus }));
            }
        }
Exemplo n.º 23
0
        public async Task <IActionResult> InitDictionaryFields(string type)
        {
            try
            {
                Product emptyProduct = await this.productManager.InitDictionaryFieldsAsync(type);

                this.responseStatus = new ResponseStatusModel {
                    Success = true
                };
                return(Ok(new { ResponseStatus = this.responseStatus, Product = emptyProduct }));
            }
            catch (ArgumentException ex)
            {
                this.responseStatus = new ResponseStatusModel {
                    Success = false, Message = ex.Message, Code = 400
                };
                return(BadRequest(new { ResponseStatus = this.responseStatus }));
            }
        }
Exemplo n.º 24
0
        public async Task <IActionResult> ImageUpload(Guid id, IFormFile file)
        {
            try
            {
                string imageID = await this.productManager.UpdateProductImageAsync(id, file);

                this.responseStatus = new ResponseStatusModel {
                    Success = true
                };
                return(Ok(new { ResponseStatus = this.responseStatus, ImageID = imageID }));
            }
            catch (ArgumentException ex)
            {
                this.responseStatus = new ResponseStatusModel {
                    Success = false, Message = ex.Message, Code = 400
                };
                return(BadRequest(new { ResponseStatus = this.responseStatus }));
            }
        }
Exemplo n.º 25
0
        public async Task <IActionResult> GetOrder(Guid id)
        {
            try
            {
                Order order = await this.orderManager.GetOrderAsync(id);

                this.responseStatus = new ResponseStatusModel {
                    Success = true
                };
                return(Ok(new { ResponseStatus = this.responseStatus, Order = order }));
            }
            catch (ArgumentException ex)
            {
                this.responseStatus = new ResponseStatusModel {
                    Success = true, Message = ex.Message, Code = 404
                };
                return(NotFound(new { ResponseStatus = this.responseStatus }));
            }
        }
Exemplo n.º 26
0
        public async Task <IActionResult> GetProduct(int id)
        {
            ProductDto product = await this.productManager.GetProduct(id);

            if (product != null)
            {
                this.responseStatus = new ResponseStatusModel {
                    Success = true
                };
                return(Ok(new { ResponseStatus = this.responseStatus, Product = product }));
            }
            else
            {
                this.responseStatus = new ResponseStatusModel {
                    Code = 404, Message = $"Produc with id - {id} not Found", Success = false
                };
                return(NotFound(new { ResponseStatus = this.responseStatus }));
            }
        }
Exemplo n.º 27
0
        public async Task <IActionResult> ConfirmOrder(int id)
        {
            try
            {
                await this.orderManager.ConfirmOrder(id);

                this.responseStatus = new ResponseStatusModel {
                    Success = true
                };
                return(Ok(new { ResponseStatus = this.responseStatus }));
            }
            catch (ArgumentException ex)
            {
                this.responseStatus = new ResponseStatusModel {
                    Success = true, Message = ex.Message, Code = 404
                };
                return(NotFound(new { ResponseStatus = this.responseStatus }));
            }
        }
        public async Task <IActionResult> Registration([FromBody] RegistrationRequestModel model)
        {
            if (await this.userManager.FindByLoginAsync(model.Login) != null)
            {
                this.responseStatus = new ResponseStatusModel {
                    Success = false, Message = "This login already use.", Code = 409
                };
                return(StatusCode(409, new { ResponseStatus = this.responseStatus }));
            }

            if (await this.userManager.FindByEmailAsync(model.Email) != null)
            {
                this.responseStatus = new ResponseStatusModel {
                    Success = false, Message = "This email already use.", Code = 409
                };
                return(StatusCode(409, new { ResponseStatus = this.responseStatus }));
            }

            User user = this.userManager
                        .CreateUser(new User
            {
                Login    = model.Login,
                Email    = model.Email,
                Password = model.Password,
                Country  = model.Country,
                Year     = model.Year,
                Roles    = new string[] { "user" }
            });

            if (user != null)
            {
                this.responseStatus = new ResponseStatusModel {
                    Success = true
                };
                return(Ok(new { ResponseStatus = this.responseStatus }));
            }

            this.responseStatus = new ResponseStatusModel {
                Success = false, Code = 400
            };
            return(BadRequest(new { ResponseStatus = this.responseStatus }));
        }
        public async Task <IActionResult> Checkout([FromBody] CheckoutRequestModel request)
        {
            try
            {
                await this.cartManager.CheckoutAsync(new User
                {
                    _Id             = request._Id,
                    Name            = request.Name,
                    Phone           = request.Phone,
                    DeliveryAddress = request.DeliveryAddress,
                });

                this.responseStatus = new ResponseStatusModel {
                    Success = true
                };
                return(Ok(new { ResponseStatus = this.responseStatus }));
            }
            catch (ArgumentException ex)
            {
                return(NotFound(new { Success = false, Error = ex.Message }));
            }
        }
        public async Task <IActionResult> GetUserDataForCheckout()
        {
            string token  = Request.Headers.FirstOrDefault(h => h.Key == "Authorization").Value;
            string userID = JWTHelper.GetClaimData(token.Remove(0, token.LastIndexOf(' ') + 1), ClaimsTypeConst.ID);

            User user = await this.userManager.GetUserDataForCheckoutAsync(userID);

            if (user != null)
            {
                this.responseStatus = new ResponseStatusModel {
                    Success = true
                };
                return(Ok(new { ResponseStatus = this.responseStatus, User = user }));
            }
            else
            {
                this.responseStatus = new ResponseStatusModel {
                    Success = false, Message = "User data not found", Code = 400
                };
                return(NotFound(new { ResponseStatus = this.responseStatus }));
            }
        }