Ejemplo n.º 1
0
        /// <summary>
        /// Create a new Store's default Payment Type
        /// </summary>
        public bool Create(Objects.Store store)
        {
            bool response = false;
            var  storeId  = store.Id ?? Properties.Settings.Default.StoreId;

            try
            {
                //Open MySqlConnection
                Database.Connection.Open();
                var query = "INSERT INTO PaymentTypes (StoreId) VALUES (@StoreId)";

                //Execute Query With MySqlConnection
                MySqlCommand Command = new MySqlCommand(query, Database.Connection);
                Command.Parameters.AddWithValue("StoreId", storeId);
                Command.ExecuteNonQuery();

                //Close Connection
                Database.Connection.Close();

                return(true);
            }
            catch (MySqlException e)
            {
                Logger.QueryError(e, "Payment Type", "Create");
            }

            //Close Connection if Open
            if (Database.Connection.State == ConnectionState.Open)
            {
                Database.Connection.Close();
            }

            return(response);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Authentificate Store
        /// </summary>
        /// <param name="email"> Store Owner Email</param>
        /// <param name="password"> Store Owner Password </param>
        /// <returns></returns>
        public Response Login(Objects.Store store)
        {
            int statusCode = 201;
            //Props
            Response response = new Response();

            try
            {
                //Open Connection
                Database.Connection.Open();

                //Query
                string loginQuery = "SELECT StoreId FROM Stores WHERE Email=@Email AND Password=@Password";

                //Execute Query With MySqlConnection  => DataReader
                MySqlCommand Command = new MySqlCommand(loginQuery, Database.Connection);

                //Parameters For Security Purpose
                Command.Parameters.AddWithValue("Email", store.Email);
                Command.Parameters.AddWithValue("Password", store.Password);

                MySqlDataReader DataReader = Command.ExecuteReader();
                //Read Data
                while (DataReader.Read())
                {
                    store.Id = DataReader["StoreId"].ToString();
                }

                if (!DataReader.HasRows)
                {
                    // Bad Request
                    statusCode = 400;
                }
                else
                {
                    // OK
                    statusCode = 200;
                }
                //Close Connection
                Database.Connection.Close();

                //Response
                response.StatusCode = statusCode;
                response.Data       = store;
            }
            catch (MySqlException e)
            {
                Logger.QueryError(e, "Store", "Login");
                // Internal Error :
                response.StatusCode = 500;
            }
            //Close Connection if Open
            if (Database.Connection.State == ConnectionState.Open)
            {
                Database.Connection.Close();
            }
            return(response);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Create a new Store's default Taxes (2)
        /// </summary>
        public bool Create(Objects.Store store = null)
        {
            bool response = false;
            var  storeId  = store.Id ?? Properties.Settings.Default.StoreId;
            var  taxId    = ServerUtil.GenerateToken(TokenLengths.TokensLength);
            var  _taxId   = ServerUtil.GenerateToken(TokenLengths.TokensLength);

            try
            {
                //Open MySqlConnection
                Database.Connection.Open();
                var query1 = "INSERT INTO Taxes (TaxId, StoreId) VALUES (@TaxId, @StoreId)";
                var query2 = "INSERT INTO Taxes (TaxId, StoreId) VALUES (@TaxId, @StoreId)";

                //Execute Query With MySqlConnection
                MySqlCommand Command1 = new MySqlCommand(query1, Database.Connection);
                MySqlCommand Command2 = new MySqlCommand(query2, Database.Connection);
                Command1.Parameters.AddWithValue("TaxId", taxId);
                Command1.Parameters.AddWithValue("StoreId", storeId);
                Command2.Parameters.AddWithValue("TaxId", _taxId);
                Command2.Parameters.AddWithValue("StoreId", storeId);
                Command1.ExecuteNonQuery();
                Command2.ExecuteNonQuery();

                //Close Connection
                Database.Connection.Close();

                return(true);
            }
            catch (MySqlException e)
            {
                Logger.QueryError(e, "Tax", "Create");
            }

            //Close Connection if Open
            if (Database.Connection.State == ConnectionState.Open)
            {
                Database.Connection.Close();
            }

            return(response);
        }
Ejemplo n.º 4
0
            /// <summary>
            /// Get Store properties from settings
            /// </summary>
            /// <param name="store"> Store Object </param>
            public static void SynchronizeStore(string storeId = null)
            {
                Objects.Store store = new Objects.Store
                {
                    Id = storeId ?? Properties.Settings.Default.StoreId
                };
                if (Validation.IsTokenValid(store.Id))
                {
                    var response = QueryController.Store.Get(store);
                    // Check response StatusCode
                    if (response.StatusCode == 200)
                    {
                        var _store = (Objects.Store)response.Data;
                        Properties.Settings.Default.StoreId = _store.Id;
                        var storeEncoded = Object.ToString(_store);
                        // Update Store Infos
                        Properties.Settings.Default.Store = storeEncoded;
                    }
                }

                Properties.Settings.Default.Save();
            }
Ejemplo n.º 5
0
        /// <summary>
        /// Get PaymentType
        /// </summary>
        public PaymentTypes Get(Objects.Store store)
        {
            store.Id = store.Id ?? Properties.Settings.Default.StoreId;
            PaymentTypes paymentTypes = new PaymentTypes();

            try
            {
                Database.Connection.Open();

                string Query = "SELECT * FROM PaymentTypes WHERE StoreId=@StoreId";

                MySqlCommand Command = new MySqlCommand(Query, Database.Connection);

                Command.Parameters.AddWithValue("StoreId", store.Id);
                MySqlDataReader DataReader = Command.ExecuteReader();

                //Read Data
                while (DataReader.Read())
                {
                    paymentTypes.Cash        = Func.ToBoolean(DataReader["Cash"].ToString());
                    paymentTypes.CreditCard  = Func.ToBoolean(DataReader["CreditCard"].ToString());
                    paymentTypes.StoreCredit = Func.ToBoolean(DataReader["StoreCredit"].ToString());
                }


                Database.Connection.Close();
            }
            catch (MySqlException e)
            {
                Logger.QueryError(e, "PaymentType", "Get");
            }
            if (Database.Connection.State == ConnectionState.Open)
            {
                Database.Connection.Close();
            }

            return(paymentTypes);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Update Payment Type
        /// </summary>
        public bool Update(Objects.Store store)
        {
            bool response = false;

            var storeId            = store.Id ?? Properties.Settings.Default.StoreId;
            var creditCardPayment  = Func.ToString(store.PaymentTypes.CreditCard);
            var storeCreditPayment = Func.ToString(store.PaymentTypes.StoreCredit);

            try
            {
                Database.Connection.Open();
                var query = "UPDATE PaymentTypes SET CreditCard=@CreditCard, StoreCredit=@StoreCredit " +
                            "WHERE StoreId=@StoreId";

                MySqlCommand Command = new MySqlCommand(query, Database.Connection);
                Command.Parameters.AddWithValue("StoreId", storeId);
                Command.Parameters.AddWithValue("CreditCard", creditCardPayment);
                Command.Parameters.AddWithValue("StoreCredit", storeCreditPayment);
                Command.ExecuteNonQuery();

                Database.Connection.Close();

                return(true);
            }
            catch (MySqlException e)
            {
                Logger.QueryError(e, "Payment Type", "Update");
            }

            if (Database.Connection.State == ConnectionState.Open)
            {
                Database.Connection.Close();
            }

            return(response);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Get Whole Registered Users if Admin
        /// </summary>
        public Response GetUsers(Objects.Store store)
        {
            //Props
            int           statusCode = 200;
            Response      response   = new Response();
            List <object> users      = new List <object>();

            store.Id = store.Id ?? Properties.Settings.Default.StoreId;

            try
            {
                Database.Connection.Open();

                string query = " SELECT DISTINCT Users.*, Blobs.Path As ProfileImageUrl " +
                               " FROM Users " +
                               " LEFT OUTER JOIN Blobs " +
                               " ON Blobs.BlobId = Users.ImageBlobId  " +
                               " ORDER BY RegisteredAt DESC ";

                //Execute Query With Connection  => DataReader
                MySqlCommand Command = new MySqlCommand(query, Database.Connection);
                Command.Parameters.AddWithValue("StoreId", store.Id);
                MySqlDataReader DataReader = Command.ExecuteReader();

                //Read Data
                while (DataReader.Read())
                {
                    Objects.User user = new Objects.User
                    {
                        Id           = DataReader["UserId"].ToString(),
                        Token        = DataReader["Token"].ToString(),
                        Username     = DataReader["Username"].ToString(),
                        Password     = DataReader["Password"].ToString(),
                        FirstName    = DataReader["FirstName"].ToString(),
                        LastName     = DataReader["LastName"].ToString(),
                        Email        = DataReader["Email"].ToString(),
                        PinCode      = DataReader["PinCode"].ToString(),
                        Role         = DataReader["Role"].ToString(),
                        Locale       = DataReader["Locale"].ToString(),
                        ImageBlobId  = DataReader["ImageBlobId"].ToString(),
                        IsOnline     = Func.ToBoolean(DataReader["IsOnline"].ToString()),
                        IsVerified   = Func.ToBoolean(DataReader["IsVerified"].ToString()),
                        Color        = ColorUtil.HEXToColor(DataReader["Color"].ToString()),
                        LastActive   = Convert.ToDateTime(DataReader["LastActive"].ToString()),
                        RegisteredAt = Convert.ToDateTime(DataReader["RegisteredAt"].ToString()),
                    };

                    user.ProfileImage = GetProfileImage(user.ImageBlobId, user.FirstName, user.Color,
                                                        doRequest: false, imageUrl: DataReader["ProfileImageUrl"].ToString());

                    users.Add(user);
                }
                if (!DataReader.HasRows)
                {
                    // Bad Request
                    statusCode = 400;
                }
                else
                {
                    // OK
                    statusCode = 200;
                }

                //Close Connection
                Database.Connection.Close();

                //Response
                response.StatusCode = statusCode;
                response.DataArray  = users;
            }
            catch (MySqlException e)
            {
                Logger.QueryError(e, "User", "Getting Users");
                // Internal Error :
                response.StatusCode = 500;
            }
            //Close Connection if Open
            if (Database.Connection.State == ConnectionState.Open)
            {
                Database.Connection.Close();
            }

            return(response);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Create a new User
        /// </summary>
        /// <param name="username"> Username</param>
        /// <param name="email"> User Email</param>
        /// <param name="password"> User Password</param>
        /// <param name="pin"> User PIN </param>
        /// <returns></returns>
        public Response Register(Objects.User user, Objects.Store store)
        {
            //Props
            int      statusCode = 201;
            Response response   = new Response();

            //Random Tokens
            string userId    = ServerUtil.GenerateToken(TokenLengths.UserIdLength);
            string userToken = ServerUtil.GenerateToken(TokenLengths.UserTokenLength);

            user.PinCode = user.PinCode ?? ServerUtil.GenerateRandomNum(minChar: 100000, maxChar: 999999).ToString();
            store.Id     = store.Id ?? Properties.Settings.Default.StoreId;
            var _color = ColorUtil.ToHEXColor(user.Color);

            try
            {
                //Open MySqlConnection
                Database.Connection.Open();
                //Query
                string query = "INSERT INTO Users (UserId, Token, StoreId, Username, Password, FirstName, LastName, Email, " +
                               " PinCode, Role, Locale, Color, ImageBlobId) " +
                               "VALUES (@UserId, @Token, @StoreId, @Username, @Password, @FirstName, @LastName, @Email, " +
                               " @PinCode, @Role, @Locale, @Color, @ImageBlobId)";

                //Execute Query With MySqlConnection
                MySqlCommand Command = new MySqlCommand(query, Database.Connection);

                //Parameters For Security Purpose
                Command.Parameters.AddWithValue("UserId", userId);
                Command.Parameters.AddWithValue("Token", userToken);
                Command.Parameters.AddWithValue("StoreId", store.Id);
                Command.Parameters.AddWithValue("Username", user.Username);
                Command.Parameters.AddWithValue("Password", user.Password);
                Command.Parameters.AddWithValue("FirstName", user.FirstName);
                Command.Parameters.AddWithValue("LastName", user.LastName);
                Command.Parameters.AddWithValue("Email", user.Email);
                Command.Parameters.AddWithValue("PinCode", user.PinCode);
                Command.Parameters.AddWithValue("Role", user.Role);
                Command.Parameters.AddWithValue("Locale", user.Locale);
                Command.Parameters.AddWithValue("Color", _color);
                Command.Parameters.AddWithValue("ImageBlobId", user.ImageBlobId);

                Command.ExecuteNonQuery();

                //Close Connection
                Database.Connection.Close();

                // Status Code
                statusCode = 201;
            }
            catch (MySqlException e)
            {
                if (e.Message.Contains("Duplicate entry"))
                {
                    //Duplicate Key or Conflict
                    statusCode = 409;
                }
                else
                {
                    // Internal Server Error
                    statusCode = 500;
                }
                Logger.QueryError(e, "User", "Register");
            }
            //Close Connection if Open
            if (Database.Connection.State == ConnectionState.Open)
            {
                Database.Connection.Close();
            }

            //Response

            response.StatusCode = statusCode;
            response.Data       = user;

            return(response);
        }
Ejemplo n.º 9
0
        /// <summary>
        ///  Update Store
        /// </summary>
        /// <returns></returns>
        public Response Update(Objects.Store store)
        {
            //Props
            int      statusCode      = 204;
            Response response        = new Response();
            var      isEmailVerified = Func.ToString(store.IsEmailVerified);
            var      isVerified      = Func.ToString(store.IsVerified);

            try
            {
                //Open Connection
                Database.Connection.Open();

                //Query
                string query = " UPDATE Stores SET Name=@Name, Email=@Email, Password=@Password, FirstName=@FirstName, LastName=@LastName, " +
                               " PhoneNumber=@PhoneNumber, Type=@Type, Locale=@Locale, Currency=@Currency, Address=@Address, PostCode=@PostCode, " +
                               " City=@City, Country=@Country, Status=@Status, IsEmailVerified=@IsEmailVerified, IsVerified=@IsVerified, VerificationToken=@VerificationToken " +
                               " WHERE StoreId=@StoreId";

                //Execute Query With MySqlConnection  => DataReader
                MySqlCommand Command = new MySqlCommand(query, Database.Connection);

                //Parameters For Security Purpose
                Command.Parameters.AddWithValue("StoreId", store.Id);
                Command.Parameters.AddWithValue("Name", store.Name);
                Command.Parameters.AddWithValue("Email", store.Email);
                Command.Parameters.AddWithValue("Password", store.Password);
                Command.Parameters.AddWithValue("FirstName", store.FirstName);
                Command.Parameters.AddWithValue("LastName", store.LastName);
                Command.Parameters.AddWithValue("PhoneNumber", store.PhoneNumber);
                Command.Parameters.AddWithValue("Type", store.Type);
                Command.Parameters.AddWithValue("Locale", store.Locale);
                Command.Parameters.AddWithValue("Currency", store.Currency);
                Command.Parameters.AddWithValue("Address", store.Address);
                Command.Parameters.AddWithValue("PostCode", store.PostCode);
                Command.Parameters.AddWithValue("City", store.City);
                Command.Parameters.AddWithValue("Country", store.Country);

                Command.Parameters.AddWithValue("Status", store.Status);
                Command.Parameters.AddWithValue("IsEmailVerified", isEmailVerified);
                Command.Parameters.AddWithValue("IsVerified", isVerified);

                Command.Parameters.AddWithValue("VerificationToken", store.VerificationToken);
                Command.ExecuteNonQuery();

                //Close Connection
                Database.Connection.Close();

                // Updated Successfully
                statusCode = 204;

                //Response
                response.StatusCode = statusCode;
                response.Data       = store;
            }
            catch (MySqlException e)
            {
                if (e.Message.Contains("Duplicate entry"))
                {
                    //Duplicate Key or Conflict
                    statusCode = 409;
                }
                else
                {
                    // Internal Server Error
                    statusCode = 500;
                }

                Logger.QueryError(e, "Store", "Updating Store");
                // Status Code
                response.StatusCode = statusCode;
            }
            //Close Connection if Open
            if (Database.Connection.State == ConnectionState.Open)
            {
                Database.Connection.Close();
            }

            return(response);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Get Store
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public Response Get(Objects.Store store)
        {
            //Props
            int statusCode = 200;

            store.Id = store.Id ?? Properties.Settings.Default.StoreId;
            Response response = new Response();

            try
            {
                //Open MySqlConnection
                Database.Connection.Open();

                //Query
                string Query = "SELECT * FROM Stores WHERE StoreId=@StoreId";

                //Execute Query With MySqlConnection  => DataReader
                MySqlCommand Command = new MySqlCommand(Query, Database.Connection);

                //Parameters For Security Purpose
                Command.Parameters.AddWithValue("StoreId", store.Id);
                MySqlDataReader DataReader = Command.ExecuteReader();

                //Read Data
                while (DataReader.Read())
                {
                    store.Id          = DataReader["StoreId"].ToString();
                    store.Name        = DataReader["Name"].ToString();
                    store.Email       = DataReader["Email"].ToString();
                    store.Password    = DataReader["Password"].ToString();
                    store.FirstName   = DataReader["FirstName"].ToString();
                    store.LastName    = DataReader["LastName"].ToString();
                    store.PhoneNumber = DataReader["PhoneNumber"].ToString();
                    store.Type        = DataReader["Type"].ToString();
                    store.Locale      = DataReader["Locale"].ToString();
                    store.Currency    = DataReader["Currency"].ToString();
                    store.Address     = DataReader["Address"].ToString();
                    store.PostCode    = DataReader["PostCode"].ToString();
                    store.City        = DataReader["City"].ToString();
                    store.Country     = DataReader["Country"].ToString();
                    store.Status      = DataReader["Status"].ToString();

                    store.IsEmailVerified = Func.ToBoolean(DataReader["IsEmailVerified"].ToString());
                    store.IsVerified      = Func.ToBoolean(DataReader["IsVerified"].ToString());

                    store.VerificationToken = DataReader["VerificationToken"].ToString();
                    store.UpdatedAt         = Convert.ToDateTime(DataReader["UpdatedAt"].ToString());
                    store.RegisteredAt      = Convert.ToDateTime(DataReader["RegisteredAt"].ToString());
                }
                if (!DataReader.HasRows)
                {
                    // Bad Request
                    statusCode = 400;
                }
                else
                {
                    // OK
                    statusCode = 200;
                }

                //Close Connection
                Database.Connection.Close();

                store.PaymentTypes = paymentType.Get(store);

                //Response
                response.StatusCode = statusCode;
                response.Data       = store;
            }
            catch (MySqlException e)
            {
                Logger.QueryError(e, "Store", "Getting Store");
                // Internal Error :
                response.StatusCode = 500;
            }
            //Close Connection if Open
            if (Database.Connection.State == ConnectionState.Open)
            {
                Database.Connection.Close();
            }

            return(response);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Create a new Store
        /// </summary>
        /// <param name="storeName"> Store Name</param>
        /// <param name="email"> Store Owner Email</param>
        /// <param name="password"> Store Owner Password</param>
        /// <returns></returns>
        public Response Register(Objects.Store store)
        {
            //Props
            int      statusCode = 201;
            Response response   = new Response();
            //Random Tokens
            string storeId           = ServerUtil.GenerateToken(TokenLengths.StoreIdLength);
            string verificationToken = ServerUtil.GenerateToken(TokenLengths.VerificationTokenLength);

            try
            {
                //Open MySqlConnection
                Database.Connection.Open();

                //Query
                string query = "INSERT INTO Stores (StoreId, Name, Email, Password, FirstName, LastName, PhoneNumber, Type, Locale, " +
                               " Address, PostCode, City, Country, VerificationToken) " +
                               "VALUES (@StoreId, @Name, @Email, @Password, @FirstName, @LastName, @PhoneNumber, @Type, @Locale, " +
                               " @Address, @PostCode, @City, @Country, @VerificationToken)";

                //Execute Query With MySqlConnection
                MySqlCommand Command = new MySqlCommand(query, Database.Connection);

                //Parameters For Security Purpose
                Command.Parameters.AddWithValue("StoreId", storeId);
                Command.Parameters.AddWithValue("Name", store.Name);
                Command.Parameters.AddWithValue("Email", store.Email);
                Command.Parameters.AddWithValue("Password", store.Password);
                Command.Parameters.AddWithValue("FirstName", store.FirstName);
                Command.Parameters.AddWithValue("LastName", store.LastName);
                Command.Parameters.AddWithValue("PhoneNumber", store.PhoneNumber);
                Command.Parameters.AddWithValue("Type", store.Type);
                Command.Parameters.AddWithValue("Locale", store.Locale);
                Command.Parameters.AddWithValue("Address", store.Address);
                Command.Parameters.AddWithValue("PostCode", store.PostCode);
                Command.Parameters.AddWithValue("City", store.City);
                Command.Parameters.AddWithValue("Country", store.Country);
                Command.Parameters.AddWithValue("VerificationToken", verificationToken);

                Command.ExecuteNonQuery();

                //Close Connection
                Database.Connection.Close();

                paymentType.Create(store);

                // Status Code
                statusCode = 201;

                store.Id = storeId;
                store.VerificationToken = verificationToken;

                //Response
                response.StatusCode = statusCode;
                response.Data       = store;
            }
            catch (MySqlException e)
            {
                if (e.Message.Contains("Duplicate entry"))
                {
                    //Duplicate Key or Conflict
                    statusCode = 409;
                }
                else
                {
                    // Internal Server Error
                    statusCode = 500;
                }
                Logger.QueryError(e, "Store", "Register");

                // Status Code
                response.StatusCode = statusCode;
            }
            //Close Connection if Open
            if (Database.Connection.State == ConnectionState.Open)
            {
                Database.Connection.Close();
            }

            return(response);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Get All Store taxes
        /// </summary>
        /// <param name="store"></param>
        /// <returns></returns>
        public Response GetTaxes(Objects.Store store)
        {
            //Props
            int statusCode = 200;

            store.Id = store.Id ?? Properties.Settings.Default.StoreId;
            Response      response = new Response();
            List <object> taxes    = new List <object>();

            try
            {
                Database.Connection.Open();
                //Query
                string query = "SELECT * FROM Taxes WHERE StoreId=@StoreId";

                //Execute Query
                MySqlCommand Command = new MySqlCommand(query, Database.Connection);
                Command.Parameters.AddWithValue("StoreId", store.Id);
                MySqlDataReader DataReader = Command.ExecuteReader();

                //Read Data
                while (DataReader.Read())
                {
                    Objects.Tax tax = new Objects.Tax
                    {
                        Id             = DataReader["TaxId"].ToString(),
                        Name           = DataReader["Name"].ToString(),
                        Rate           = decimal.Parse(DataReader["Rate"].ToString()),
                        ShortName      = DataReader["ShortName"].ToString(),
                        StoreShortName = DataReader["StoreShortName"].ToString(),
                        Description    = DataReader["Description"].ToString(),
                        UpdatedAt      = Convert.ToDateTime(DataReader["UpdatedAt"].ToString()),
                        CreatedAt      = Convert.ToDateTime(DataReader["CreatedAt"].ToString())
                    };

                    taxes.Add(tax);
                }
                if (!DataReader.HasRows)
                {
                    // Bad Request
                    statusCode = 400;
                }
                else
                {
                    // OK
                    statusCode = 200;
                }

                //Close Connection
                Database.Connection.Close();

                response.StatusCode = statusCode;
                response.DataArray  = taxes;
            }
            catch (MySqlException e)
            {
                Logger.QueryError(e, "Taxes", "Getting Taxes");
                response.StatusCode = 500;
            }
            //Close Connection if Open
            if (Database.Connection.State == ConnectionState.Open)
            {
                Database.Connection.Close();
            }

            return(response);
        }