示例#1
0
        public async Task <IActionResult> UpdateEmployee([FromRoute] string id, [FromBody] BusinessEmployeeViewModel employeeModel)
        {
            var errorMessage = new ErrorMessageViewModel();
            var error        = new { Error = errorMessage };

            // Get the employee from the database
            BusinessEmployees employee = db.BusinessEmployees.Where(e => e.UserId == id).SingleOrDefault();

            if (employee == null)
            {
                errorMessage.Message = "Could not find employee";
                return(Json(error));
            }

            ApplicationUser user = db.ApplicationUser.Where(e => e.Id == id).SingleOrDefault();

            if (user == null)
            {
                errorMessage.Message = "Could not find user profile";
                return(Json(error));
            }

            // Update information
            employee.FirstName      = employeeModel.FirstName;
            employee.LastName       = employeeModel.LastName;
            employee.Position       = employeeModel.Position;
            employee.CanEditLibrary = employeeModel.CanEditLibrary;

            user.Email = employeeModel.Email;

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

            try
            {
                await _userManager.UpdateNormalizedEmailAsync(user);

                db.SaveChanges();
            }
            catch (Exception exception)
            {
                errorMessage.Message = "Could not update employee information";
                return(Json(error));
            }

            // Model data to return to view
            var result = new BusinessEmployeeViewModel
            {
                Id             = employee.UserId,
                FirstName      = employee.FirstName,
                LastName       = employee.LastName,
                Position       = employee.Position,
                ProfilePicture = employee.ProfilePicture,
                CanEditLibrary = employee.CanEditLibrary,
                Email          = user.Email
            };

            return(Ok(result));
        }
示例#2
0
        public async Task <IActionResult> UpdateProfile([FromRoute] string id, [FromBody] ProfileDetailsViewModel profile)
        {
            var errorMessage = new ErrorMessageViewModel();
            var error        = new { Error = errorMessage };

            var role = "";

            if (User != null)
            {
                try
                {
                    role = User.Claims.Where(c => c.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/role").SingleOrDefault().Value;

                    if (role == null)
                    {
                        errorMessage.Message = "Could not find role for user";
                        return(Json(error));
                    }
                }
                catch (Exception exception)
                {
                    errorMessage.Message = "Could not get role for user";
                    return(Json(error));
                }
            }

            if (profile == null)
            {
                errorMessage.Message = "Model is missing data";
                return(Json(error));
            }

            // Find the type of user based on the role
            if (role == "Personal")
            {
                // Get the personal user in the database
                PersonalUsers personalUser = db.PersonalUsers.Where(e => e.UserId == id).SingleOrDefault();

                // Update the details for the profile
                if (personalUser != null)
                {
                    personalUser.FirstName = profile.FirstName;
                    personalUser.LastName  = profile.LastName;

                    if (!String.IsNullOrWhiteSpace(profile.ProfilePicture))
                    {
                        var fileName = await fileController.UploadImage(profile.ProfilePicture, Request);

                        if (String.IsNullOrWhiteSpace(fileName))
                        {
                            errorMessage.Message = "Image upload encountered an error";
                            return(Json(error));
                        }

                        personalUser.ProfilePicture = fileName;
                    }

                    // Update record in the database
                    db.Entry(personalUser).State = EntityState.Modified;

                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception exception)
                    {
                        errorMessage.Message = "Could update the account information";
                        return(Json(error));
                    }

                    return(Ok(personalUser));
                }
                else
                {
                    errorMessage.Message = "Could not find the user profile";
                    return(Json(error));
                }
            }
            else if (role == "Business")
            {
                // Get the business user in the database
                BusinessUsers businessUser = db.BusinessUsers.Where(e => e.UserId == id).SingleOrDefault();

                // Update the details for the profile
                if (businessUser != null)
                {
                    businessUser.FirstName    = profile.FirstName;
                    businessUser.LastName     = profile.LastName;
                    businessUser.Organization = profile.Organization;
                    businessUser.PhoneNumber  = profile.PhoneNumber;

                    if (!String.IsNullOrWhiteSpace(profile.ProfilePicture))
                    {
                        var fileName = await fileController.UploadImage(profile.ProfilePicture, Request);

                        if (String.IsNullOrWhiteSpace(fileName))
                        {
                            errorMessage.Message = "Image upload encountered an error";
                            return(Json(error));
                        }

                        businessUser.ProfilePicture = fileName;
                    }

                    // Update record in the database
                    db.Entry(businessUser).State = EntityState.Modified;

                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception exception)
                    {
                        errorMessage.Message = "Could update the account information";
                        return(Json(error));
                    }

                    return(Ok(businessUser));
                }
                else
                {
                    errorMessage.Message = "Could not find the user profile";
                    return(Json(error));
                }
            }
            else if (role == "Employee")
            {
                // Get the employee in the database
                BusinessEmployees employee = db.BusinessEmployees.Where(e => e.UserId == id).SingleOrDefault();

                // Update the details for the profile
                if (employee != null)
                {
                    employee.FirstName   = profile.FirstName;
                    employee.LastName    = profile.LastName;
                    employee.PhoneNumber = profile.PhoneNumber;

                    if (!String.IsNullOrWhiteSpace(profile.ProfilePicture))
                    {
                        var fileName = await fileController.UploadImage(profile.ProfilePicture, Request);

                        if (String.IsNullOrWhiteSpace(fileName))
                        {
                            errorMessage.Message = "Image upload encountered an error";
                            return(Json(error));
                        }

                        employee.ProfilePicture = fileName;
                    }

                    // Update record in the database
                    db.Entry(employee).State = EntityState.Modified;

                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception exception)
                    {
                        errorMessage.Message = "Could update the account information";
                        return(Json(error));
                    }

                    return(Ok(employee));
                }
                else
                {
                    errorMessage.Message = "Could not find the user profile";
                    return(Json(error));
                }
            }

            errorMessage.Message = "An error has occurred";
            return(Json(error));
        }
示例#3
0
        public async Task <IActionResult> CheckoutBook([FromRoute] int id, [FromBody] CheckoutViewModel checkoutBook)
        {
            var errorMessage = new ErrorMessageViewModel();
            var error        = new { Error = errorMessage };
            var userId       = "";

            if (!ModelState.IsValid)
            {
                errorMessage.Message = "The model is missing required fields";
                return(Json(error));
            }

            if (User == null)
            {
                errorMessage.Message = "Could not find user for claims";
                return(Json(error));
            }

            try
            {
                userId = User.Claims.Where(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").SingleOrDefault().Value;
            }
            catch (Exception exception)
            {
                errorMessage.Message = "User Id was not found";
                return(Json(error));
            }

            // Get book and check out
            Documents book = db.Documents.Where(e => e.DocumentId == id).SingleOrDefault();

            if (book == null)
            {
                errorMessage.Message = "Could not find book";
                return(Json(error));
            }

            book.CheckedOut      = true;
            db.Entry(book).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (Exception exception)
            {
                errorMessage.Message = "Could not check out book";
                return(Json(error));
            }

            // Add book to checked out in database
            var checkedOutBook = new Checkouts
            {
                BookId      = id,
                UserId      = userId,
                Email       = checkoutBook.Email,
                Name        = checkoutBook.Name,
                PhoneNumber = checkoutBook.PhoneNumber,
                ReturnDate  = checkoutBook.ReturnDate,
                CheckedOut  = true
            };

            db.Checkouts.Add(checkedOutBook);

            try
            {
                db.SaveChanges();
            }
            catch (Exception exception)
            {
                errorMessage.Message = "Could not add checked out book to database";
                return(Json(error));
            }

            return(Ok(checkedOutBook));
        }