public async Task <IActionResult> GetUserProfile([FromRoute] string id) { var errorMessage = new ErrorMessageViewModel(); var error = new { Error = errorMessage }; // Get the user profile ApplicationUser user = db.ApplicationUser.Where(e => e.Id == id).SingleOrDefault(); if (user == null) { errorMessage.Message = "Could not find the user profile"; return(Json(error)); } // Find the user type based on the id BusinessUsers businessUser = db.BusinessUsers.Where(e => e.UserId == id).SingleOrDefault(); if (businessUser == null) { BusinessEmployees employee = db.BusinessEmployees.Where(e => e.UserId == id).SingleOrDefault(); if (employee == null) { PersonalUsers personalUser = db.PersonalUsers.Where(e => e.UserId == id).SingleOrDefault(); if (personalUser == null) { errorMessage.Message = "Could not find the profile for the user"; return(Json(error)); } // Get the personal user details ProfileDetailsViewModel personalProfile = new ProfileDetailsViewModel { Id = personalUser.UserId, FirstName = personalUser.FirstName, LastName = personalUser.LastName, Email = user.Email, AccountType = "Personal", ProfilePicture = personalUser.ProfilePicture }; return(Ok(personalProfile)); } // Get the employer info BusinessUsers employer = db.BusinessUsers.Where(e => e.BusinessUserId == employee.BusinessUserId).SingleOrDefault(); if (employer == null) { errorMessage.Message = "Could not find the employer profile for the employee"; return(Json(error)); } // Get the employee user details EmployeeDetailsViewModel employeeProfile = new EmployeeDetailsViewModel { Id = employee.UserId, FirstName = employee.FirstName, LastName = employee.LastName, Email = user.Email, AccountType = "Employee", ProfilePicture = employee.ProfilePicture, Organization = employer.Organization, CanEditLibrary = employee.CanEditLibrary }; return(Ok(employeeProfile)); } // Get the business user details ProfileDetailsViewModel businessProfile = new ProfileDetailsViewModel { Id = businessUser.UserId, FirstName = businessUser.FirstName, LastName = businessUser.LastName, Email = user.Email, AccountType = "Business", ProfilePicture = businessUser.ProfilePicture, Organization = businessUser.Organization }; return(Ok(businessProfile)); }
public async Task <object> Register([FromBody] RegistrationViewModel model) { var errorMessage = new ErrorMessageViewModel(); var error = new { Error = errorMessage }; // Check for the role type upon registration string[] availableRoles = { "Personal", "Business" }; if (!availableRoles.Contains(model.Role)) { errorMessage.Message = "Invalid Role"; return(Json(error)); } // Create variable for Application User var user = new ApplicationUser { Email = model.Email, UserName = model.Email }; ApplicationUser appUser = db.ApplicationUser.Where(e => e.Email.Equals(user.Email, StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault(); if (appUser != null) { errorMessage.Message = "Username is already taken."; return(Json(error)); } var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { await _userManager.AddToRoleAsync(user, model.Role); await _signInManager.SignInAsync(user, false); // Profile Types BusinessUsers businessUser = new BusinessUsers(); PersonalUsers personalUser = new PersonalUsers(); // Create the user profile if (model == null) { errorMessage.Message = "No data was found"; return(Json(error)); } if (model.Role == availableRoles[0]) { // If the user role is a business user var profile = new PersonalUsers { FirstName = model.FirstName, LastName = model.LastName, UserId = user.Id }; // Add the user to the database db.PersonalUsers.Add(profile); try { db.SaveChanges(); } catch (Exception exception) { errorMessage.Message = "Could not add personal user to the database"; return(Json(error)); } personalUser = profile; } else if (model.Role == availableRoles[1]) { // If the user role is a personal user var profile = new BusinessUsers { FirstName = model.FirstName, LastName = model.LastName, Organization = model.Organization, UserId = user.Id, PhoneNumber = model.PhoneNumber }; // Add the user to the database db.BusinessUsers.Add(profile); try { db.SaveChanges(); } catch (Exception exception) { errorMessage.Message = "Could not add business user to the database"; return(Json(error)); } businessUser = profile; } // Create role list to create jwt token List <string> roleList = new List <string>() { model.Role }; var jwtToken = await GenerateJwtToken(model.Email, user, roleList); var userRoles = await _userManager.GetRolesAsync(user); // Send the user role along with the JWT Token if (model.Role == availableRoles[0]) { var response = new PersonalLoginResponse { Token = jwtToken.ToString(), Roles = userRoles.ToArray(), PersonalUser = personalUser }; return(Json(response)); } else if (model.Role == availableRoles[1]) { var response = new BusinessLoginResponse { Token = jwtToken.ToString(), Roles = userRoles.ToArray(), BusinessUser = businessUser }; return(Json(response)); } } else if (!result.Succeeded) { errorMessage.Message = "Password must have 6+ characters, at least 1 uppercase character, 1 lowercase character, 1 number, and 1 non-alphanumeric character"; return(Json(error)); } errorMessage.Message = "An error has occurred"; return(Json(error)); }
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)); }
public async Task <IActionResult> GetMyBooks() { var id = ""; var role = ""; var errorMessage = new ErrorMessageViewModel(); var error = new { Error = errorMessage }; if (User == null) { errorMessage.Message = "Could not find user for claims"; return(Json(error)); } try { id = User.Claims.Where(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").SingleOrDefault().Value; role = User.Claims.Where(c => c.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/role").SingleOrDefault().Value; } catch (Exception exception) { errorMessage.Message = "Id or role was not found"; return(Json(error)); } if (role == "Personal") { PersonalUsers personalUser = db.PersonalUsers.Where(e => e.UserId == id).SingleOrDefault(); if (personalUser == null) { errorMessage.Message = "Could not find user profile"; return(Json(error)); } // Get books for the user List <BookDetailsViewModel> books = new List <BookDetailsViewModel>(); List <Documents> bookList = db.Documents.Where(e => e.UserId == id).ToList(); foreach (Documents item in bookList) { // Get the genres for the book List <DocumentGenres> genreList = db.DocumentGenres.Where(e => e.DocumentId == item.DocumentId).ToList(); List <BookGenreViewModel> genres = new List <BookGenreViewModel>(); foreach (var dbGenre in genreList) { Genres genre = db.Genres.Where(e => e.GenreId == dbGenre.GenreId).SingleOrDefault(); if (genre == null) { errorMessage.Message = "Could not find the genre for the book"; return(Json(error)); } var bookGenre = new BookGenreViewModel { GenreId = genre.GenreId, Name = genre.Title }; genres.Add(bookGenre); } // Get the authors for the book List <DocumentAuthors> authorList = db.DocumentAuthors.Where(e => e.DocumentId == item.DocumentId).ToList(); List <BookAuthorViewModel> authors = new List <BookAuthorViewModel>(); foreach (var documentAuthor in authorList) { Authors author = db.Authors.Where(e => e.AuthorId == documentAuthor.AuthorId).SingleOrDefault(); if (author == null) { errorMessage.Message = "Could not find the author for the book"; return(Json(error)); } var bookAuthor = new BookAuthorViewModel { AuthorId = author.AuthorId, Name = author.Name }; authors.Add(bookAuthor); } // Get the insurance information for the book InsuranceInformation bookInsurance = db.InsuranceInformation.Where(e => e.InsuranceInformationId == item.InsuranceInformationId).SingleOrDefault(); InsuranceInformationViewModel insuranceInformation = new InsuranceInformationViewModel(); if (bookInsurance != null) { insuranceInformation.Cost = bookInsurance.Cost; insuranceInformation.DatePurchased = bookInsurance.DatePurchased.ToString("yyyy-MM-dd"); insuranceInformation.ReceiptImage = bookInsurance.ReceiptImage; insuranceInformation.IssueDate = bookInsurance.IssueDate.ToString("yyyy-MM-dd"); } BookDetailsViewModel book = new BookDetailsViewModel { Id = item.DocumentId, Title = item.Title, Authors = authors.ToArray(), Genres = genres.ToArray(), ISBN = item.ISBN, CheckedOut = item.CheckedOut, Picture = item.CoverImage, Pages = Convert.ToInt32(item.Pages), Publisher = item.Publisher, PublishedDate = item.PublishedDate.ToString("yyyy-MM-dd"), Edition = item.Edition, Description = item.Description, InsuranceInformation = insuranceInformation }; books.Add(book); } return(Ok(books)); } else if (role == "Business") { BusinessUsers businessUser = db.BusinessUsers.Where(e => e.UserId == id).SingleOrDefault(); if (businessUser == null) { errorMessage.Message = "Could not find user profile"; return(Json(error)); } // Get books for the user List <BookDetailsViewModel> books = new List <BookDetailsViewModel>(); List <Documents> bookList = db.Documents.Where(e => e.UserId == id).ToList(); foreach (Documents item in bookList) { // Get the genres for the book List <DocumentGenres> genreList = db.DocumentGenres.Where(e => e.DocumentId == item.DocumentId).ToList(); List <BookGenreViewModel> genres = new List <BookGenreViewModel>(); foreach (var dbGenre in genreList) { Genres genre = db.Genres.Where(e => e.GenreId == dbGenre.GenreId).SingleOrDefault(); if (genre == null) { errorMessage.Message = "Could not find the genre for the book"; return(Json(error)); } var bookGenre = new BookGenreViewModel { GenreId = genre.GenreId, Name = genre.Title }; genres.Add(bookGenre); } // Get the authors for the book List <DocumentAuthors> authorList = db.DocumentAuthors.Where(e => e.DocumentId == item.DocumentId).ToList(); List <BookAuthorViewModel> authors = new List <BookAuthorViewModel>(); foreach (var documentAuthor in authorList) { Authors author = db.Authors.Where(e => e.AuthorId == documentAuthor.AuthorId).SingleOrDefault(); if (author == null) { errorMessage.Message = "Could not find the author for the book"; return(Json(error)); } var bookAuthor = new BookAuthorViewModel { AuthorId = author.AuthorId, Name = author.Name }; authors.Add(bookAuthor); } // Get the insurance information for the book InsuranceInformation bookInsurance = db.InsuranceInformation.Where(e => e.InsuranceInformationId == item.InsuranceInformationId).SingleOrDefault(); InsuranceInformationViewModel insuranceInformation = new InsuranceInformationViewModel(); if (bookInsurance != null) { insuranceInformation.Cost = bookInsurance.Cost; insuranceInformation.DatePurchased = bookInsurance.DatePurchased.ToString("yyyy-MM-dd"); insuranceInformation.ReceiptImage = bookInsurance.ReceiptImage; insuranceInformation.IssueDate = bookInsurance.IssueDate.ToString("yyyy-MM-dd"); } BookDetailsViewModel book = new BookDetailsViewModel { Id = item.DocumentId, Title = item.Title, Authors = authors.ToArray(), Genres = genres.ToArray(), ISBN = item.ISBN, CheckedOut = item.CheckedOut, Picture = item.CoverImage, Pages = Convert.ToInt32(item.Pages), Publisher = item.Publisher, PublishedDate = item.PublishedDate.ToString("yyyy-MM-dd"), Edition = item.Edition, Description = item.Description, InsuranceInformation = insuranceInformation }; books.Add(book); } return(Ok(books)); } else if (role == "Employee") { BusinessEmployees employee = db.BusinessEmployees.Where(e => e.UserId == id).SingleOrDefault(); if (employee == null) { errorMessage.Message = "Could not find user profile"; return(Json(error)); } // Get the employer BusinessUsers businessUser = db.BusinessUsers.Where(e => e.BusinessUserId == employee.BusinessUserId).SingleOrDefault(); // Get books for the user List <BookDetailsViewModel> books = new List <BookDetailsViewModel>(); List <Documents> bookList = db.Documents.Where(e => e.UserId == businessUser.UserId).ToList(); foreach (Documents item in bookList) { // Get the genres for the book List <DocumentGenres> genreList = db.DocumentGenres.Where(e => e.DocumentId == item.DocumentId).ToList(); List <BookGenreViewModel> genres = new List <BookGenreViewModel>(); foreach (var dbGenre in genreList) { Genres genre = db.Genres.Where(e => e.GenreId == dbGenre.GenreId).SingleOrDefault(); if (genre == null) { errorMessage.Message = "Could not find the genre for the book"; return(Json(error)); } var bookGenre = new BookGenreViewModel { GenreId = genre.GenreId, Name = genre.Title }; genres.Add(bookGenre); } // Get the authors for the book List <DocumentAuthors> authorList = db.DocumentAuthors.Where(e => e.DocumentId == item.DocumentId).ToList(); List <BookAuthorViewModel> authors = new List <BookAuthorViewModel>(); foreach (var documentAuthor in authorList) { Authors author = db.Authors.Where(e => e.AuthorId == documentAuthor.AuthorId).SingleOrDefault(); if (author == null) { errorMessage.Message = "Could not find the author for the book"; return(Json(error)); } var bookAuthor = new BookAuthorViewModel { AuthorId = author.AuthorId, Name = author.Name }; authors.Add(bookAuthor); } // Get the insurance information for the book InsuranceInformation bookInsurance = db.InsuranceInformation.Where(e => e.InsuranceInformationId == item.InsuranceInformationId).SingleOrDefault(); InsuranceInformationViewModel insuranceInformation = new InsuranceInformationViewModel(); if (bookInsurance != null) { insuranceInformation.Cost = bookInsurance.Cost; insuranceInformation.DatePurchased = bookInsurance.DatePurchased.ToString("yyyy-MM-dd"); insuranceInformation.ReceiptImage = bookInsurance.ReceiptImage; insuranceInformation.IssueDate = bookInsurance.IssueDate.ToString("yyyy-MM-dd"); } BookDetailsViewModel book = new BookDetailsViewModel { Id = item.DocumentId, Title = item.Title, Authors = authors.ToArray(), Genres = genres.ToArray(), ISBN = item.ISBN, CheckedOut = item.CheckedOut, Picture = item.CoverImage, Pages = Convert.ToInt32(item.Pages), Publisher = item.Publisher, PublishedDate = item.PublishedDate.ToString("yyyy-MM-dd"), Edition = item.Edition, Description = item.Description, InsuranceInformation = insuranceInformation }; books.Add(book); } return(Ok(books)); } errorMessage.Message = "An error has occurred"; return(Ok(error)); }