Exemplo n.º 1
0
        //Get Profile Data such as First Name , Last Name, Date Of Birth and Language by this get method of Profile controller.
        public HttpResponseMessage ProfileDetails()
        {
            // Fetch User ID from Basic Authentication
            string ID = Thread.CurrentPrincipal.Identity.Name;

            //Check Whether model is valid or not.
            //IF yes then pass the data from entity model(database) to Model class.
            if (ModelState.IsValid)
            {
                // Create getProfileModel object of GetProfileModel class
                ProfileModel getProfileModel = new ProfileModel();

                //Query statement for specific ID.
                var context = new WebApiEntities();
                var query   = from user in context.ProfileTables where ID == user.UserID.ToString() select user;

                // Profile is the Class of
                ProfileTable profile = query.FirstOrDefault();

                // get profile Values
                getProfileModel.Fname    = profile.FirstName;
                getProfileModel.Lname    = profile.LastName;
                getProfileModel.Language = profile.Language;
                getProfileModel.DOB      = profile.DOB.ToString();

                // Return getProfileModel class data with Ok status of HttpMessageResponse.
                return(Request.CreateResponse(HttpStatusCode.OK, getProfileModel));
            }

            //Create Error Response if Model State is not valid and Send Message to user Please login again.
            return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "please login again"));
        }
Exemplo n.º 2
0
        //public HttpResponseMessage Put(int id, [FromBody]AccountHolderDetail employee)
        //{
        //    try
        //    {
        //        using (WebApiEntities entities = new WebApiEntities())
        //        {
        //            var entity = entities.AccountHolderDetails.FirstOrDefault(e => e.Id == id);
        //            if (entity == null)
        //            {
        //                return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with id= " + id.ToString() + " not found");

        //            }
        //            else
        //            {
        //                entity.Bank_Name = employee.Bank_Name;
        //                entity.Customer_Name = employee.Customer_Name;
        //                entity.Father_Name = employee.Father_Name;
        //                entities.SaveChanges();
        //                return Request.CreateResponse(HttpStatusCode.OK, entity);
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
        //    }


        //}

        public HttpResponseMessage Put(int id, [FromUri] AccountHolderDetail employee)
        {
            try
            {
                using (WebApiEntities entities = new WebApiEntities())
                {
                    var entity = entities.AccountHolderDetails.FirstOrDefault(e => e.Id == id);
                    if (entity == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with id= " + id.ToString() + " not found"));
                    }
                    else
                    {
                        entity.Bank_Name     = employee.Bank_Name;
                        entity.Customer_Name = employee.Customer_Name;
                        entity.Father_Name   = employee.Father_Name;
                        entities.SaveChanges();
                        return(Request.CreateResponse(HttpStatusCode.OK, entity));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Exemplo n.º 3
0
        //private List<Product> Products = new List<Product>();

        public ProductController()
        {
            context = new WebApiEntities();
            //Products.Add(new Product { Id = 1, Name = "Product1", Quantity = 20 });
            //Products.Add(new Product { Id = 2, Name = "Product2", Quantity = 10 });
            //Products.Add(new Product { Id = 3, Name = "Product3", Quantity = 30 });
        }
Exemplo n.º 4
0
        public static bool Login(string username, string password)
        {
            // create context object of entity framework database
            var context = new WebApiEntities();

            //find boolean value of user credentials are true or not
            bool isValidUser = context.RegistrationTables.Any(user => user.Username.Equals(username, StringComparison.OrdinalIgnoreCase) && user.Password == password);

            // return boolean value
            return(isValidUser);
        }
Exemplo n.º 5
0
        //public IEnumerable<AccountHolderDetail> Get()

        //{
        //    using (WebApiEntities entities = new WebApiEntities())
        //    {
        //        return entities.AccountHolderDetails.ToList();
        //    }

        //}
        public HttpResponseMessage Get(int id)
        {
            using (WebApiEntities entities = new WebApiEntities())
            {
                var entity = entities.AccountHolderDetails.FirstOrDefault(e => e.Id == id);
                if (entity != null)
                {
                    return(Request.CreateResponse(HttpStatusCode.OK, entity));
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with Id= " + id.ToString() + " not found"));
                }
            }
        }
Exemplo n.º 6
0
 public bool TestConnection()
 {
     using (var db = new WebApiEntities())
     {
         DbConnection conn = db.Database.Connection;
         try
         {
             conn.Open();
             return(true);
         }
         catch
         {
             return(false);
         }
     }
 }
Exemplo n.º 7
0
 public HttpResponseMessage  Post([FromBody] AccountHolderDetail employee)
 {
     try
     {
         using (WebApiEntities entities = new WebApiEntities())
         {
             entities.AccountHolderDetails.Add(employee);
             entities.SaveChanges();
             var massage = Request.CreateResponse(HttpStatusCode.Created, employee);
             massage.Headers.Location = new Uri(Request.RequestUri + employee.Id.ToString());
             return(massage);
         }
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
 }
Exemplo n.º 8
0
        public HttpResponseMessage UserLogin(LoginModel model)
        {
            // Check Model State is valid or not
            if (!ModelState.IsValid)
            {
                return(null);
            }
            using (var context = new WebApiEntities())
            {
                bool isValidUser = context.RegistrationTables.Any(user => user.Username.Equals(model.Username, StringComparison.OrdinalIgnoreCase) && user.Password == model.Password);
                // Check whether the user is valid or not
                if (isValidUser)
                {
                    // Run Query for user selection where username are matched
                    var query = from user in context.RegistrationTables where user.Username == model.Username select user;
                    //Get first Default query Value
                    RegistrationTable userDetails = query.FirstOrDefault();
                    //create object of Generate Token Class
                    GenerateToken generateToken = new GenerateToken();
                    // Return generatedToken through CreateToken Method
                    string generatedToken = generateToken.CreateToken(userDetails.Username, userDetails.Password, userDetails.UserID);

                    // Update token value in  Database
                    if (context.RegistrationTables.Any(user => user.UserID == userDetails.UserID))
                    {
                        RegistrationTable registration = new RegistrationTable();
                        registration.Token    = generatedToken;
                        registration.UserID   = userDetails.UserID;
                        registration.Username = userDetails.Username;
                        registration.Password = userDetails.Password;
                        // Add or Update Database context
                        context.RegistrationTables.AddOrUpdate(registration);
                        context.SaveChanges();
                    }
                    // Return generated token to client
                    return(Request.CreateResponse(HttpStatusCode.OK, generatedToken));
                }
                //If Usernama and Password are invalid then return invalid username
                ModelState.AddModelError("", "Invalid username and password or Token Expired -- Please Login Again");
                // return request status for create error response
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }
Exemplo n.º 9
0
        public HttpResponseMessage Get(string gender = "All")
        {
            using (WebApiEntities entities = new WebApiEntities())
            {
                switch (gender.ToLower())
                {
                case "all":
                    return(Request.CreateResponse(HttpStatusCode.OK, entities.AccountHolderDetails.ToList()));

                case "male":
                    return(Request.CreateResponse(HttpStatusCode.OK, entities.AccountHolderDetails.Where(e => e.Gender.ToLower() == "male").ToList()));

                case "female":
                    return(Request.CreateResponse(HttpStatusCode.OK, entities.AccountHolderDetails.Where(e => e.Gender.ToLower() == "female").ToList()));

                default:
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "value for gender must be Male,Female or All." + gender + " is invalid."));
                }
            }
        }
Exemplo n.º 10
0
 public HttpResponseMessage Delete(int id)
 {
     try
     {
         using (WebApiEntities entities = new WebApiEntities())
         {
             var entity = entities.AccountHolderDetails.FirstOrDefault(e => e.Id == id);
             if (entity == null)
             {
                 return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with Id= " + id.ToString() + " not found"));
             }
             else
             {
                 entities.AccountHolderDetails.Remove(entity);
                 entities.SaveChanges();
                 return(Request.CreateResponse(HttpStatusCode.OK));
             }
         }
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
 }
Exemplo n.º 11
0
        //Update or Save Profile Values Field in database
        public HttpResponseMessage ProfileDetails(ProfileModel model)
        {
            //Check Whether Model state is valid or not.
            if (ModelState.IsValid)
            {
                var context = new WebApiEntities();

                //Create object of database Profile Table
                ProfileTable profile = new ProfileTable();

                // Split Token by - sign
                string[] tokenArray = model.GenToken.Split('-');

                // Get ID value by third element of tokenArray
                string ID = tokenArray[3];

                //convert string DOB into DateTime
                string inString = model.DOB;

                DateTime dateValue;
                if (!DateTime.TryParse(inString, out dateValue))
                {
                    Request.CreateErrorResponse(HttpStatusCode.PartialContent, "Please fill correct date of birth");
                }

                //Update profile Data for Existing User
                if (context.ProfileTables.Any(user => user.UserID.ToString() == ID))
                {
                    //Query Statement for Specific ID
                    var query = from user in context.ProfileTables where ID == user.UserID.ToString() select user.ID;
                    //Update Values
                    profile.FirstName = model.Fname;
                    profile.LastName  = model.Lname;
                    profile.DOB       = dateValue;
                    profile.UserID    = Convert.ToInt32(ID);
                    profile.Language  = model.Language;
                    profile.ID        = query.First();

                    context.ProfileTables.Attach(profile);
                    context.Entry(profile).State = System.Data.Entity.EntityState.Modified;
                    context.SaveChanges();

                    //Return Message Profile Update with Ok status
                    return(Request.CreateResponse(HttpStatusCode.OK, "Profile Updated"));
                }
                // Create New Profile data for New User
                else
                {
                    //Save new user value into database
                    profile.UserID    = Convert.ToInt32(ID);
                    profile.FirstName = model.Fname;
                    profile.LastName  = model.Lname;
                    profile.DOB       = dateValue;
                    profile.Language  = model.Language;

                    context.ProfileTables.Add(profile);
                    context.SaveChanges();

                    //Return Message Profile Save with Ok status
                    return(Request.CreateResponse(HttpStatusCode.OK, "Profile Save"));
                }
            }
            // Create Error Response with Bad Request. and send message please fill again to client
            return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "please Fill again"));
        }