public static ActionResult SaveVerificationDetails(string email, string token) { VerificationToken verification = new VerificationToken(); try { ESamhashoEntities entities = new ESamhashoEntities(); string guidCode = Guid.NewGuid().ToString(); verification = new VerificationToken { Id = guidCode, ExpiryDate = DateTime.Now.AddDays(1), UserEmail = email, UserToken = token }; entities.VerificationTokens.Add(verification); entities.SaveChanges(); return(new ActionResult { Success = true, Message = verification.Id }); } catch (Exception exception) { Dictionary <string, string> dictionary = verification.ToDictionary(); ServiceHelper.LogException(exception, dictionary, ErrorSource.UserManagement); return(new ActionResult { Success = false, Message = "Error, failed to save verification details." }); } }
public ActionResult AddCatergory(string catergoryName) { try { using (ESamhashoEntities entities = new ESamhashoEntities()) { Catergory catergory = new Catergory { IsDeleted = false, Name = catergoryName, DateCreated = DateTime.Now, DateModified = DateTime.Now }; entities.Catergories.Add(catergory); entities.SaveChanges(); return(new ActionResult { Success = true, Message = catergory.Id.ToString() }); } } catch (Exception exception) { Dictionary <string, string> dictionary = new Dictionary <string, string> { { "catergoryName", catergoryName } }; ServiceHelper.LogException(exception, dictionary, ErrorSource.Catergory); return(new ActionResult { Message = "Error, catergory failed to save, try again." }); } }
public ActionResult GetNumberOfUserOnline() { try { ActionResult authentication = ServiceHelper.CheckAuthentication(); if (!authentication.Success) { return(authentication); } int numberOfUsersOnline = Membership.GetNumberOfUsersOnline(); return(new ActionResult { Message = string.Empty, Success = true, // DataObjects = numberOfUsersOnline.ToString() }); } catch (Exception exception) { ServiceHelper.LogException(exception, new Dictionary <string, string>(), ErrorSource.Authentication); return(new ActionResult { Success = false, Message = "Error, failed to get number of users online." }); } }
public List <NotificationData> GetAllNotifications() { try { using (ESamhashoEntities entities = new ESamhashoEntities()) { List <Notification> notificationList = entities.Notifications.Where(a => !a.IsDeleted).OrderByDescending(a => a.DateCreated).ToList(); List <NotificationData> notifications = notificationList .Select(a => new NotificationData { Message = a.Message, IsDeleted = a.IsDeleted, Name = a.Name, ProductId = a.ProductId, IsRead = a.IsRead, Phone = a.Phone, DateCreated = a.DateCreated.ToLongDateString() + " " + a.DateCreated.ToLongTimeString(), Email = a.Email, Id = a.Id, NotificationTypeId = a.NotificationTypeId, FromCustomer = a.FromCustomer }).ToList(); return(notifications); } } catch (Exception exception) { ServiceHelper.LogException(exception, new Dictionary <string, string>(), ErrorSource.Notification); return(new List <NotificationData>()); } }
public ActionResult GetAllUsers() { try { ActionResult authentication = ServiceHelper.CheckAuthentication(); if (!authentication.Success) { return(authentication); } // var membershipUserCollection = Membership.GetAllUsers(); // string json = new JavaScriptSerializer().Serialize(membershipUserCollection); return(new ActionResult { Message = string.Empty, Success = true, // DataObjects = json }); } catch (Exception exception) { ServiceHelper.LogException(exception, new Dictionary <string, string>(), ErrorSource.Authentication); return(new ActionResult { Success = false, Message = "Error, failed to get all users." }); } }
public static string GenerateSecurityToken(string userId, string username, string email) { try { byte[] symmetricKey = Convert.FromBase64String(key); JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler(); SecurityTokenDescriptor tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new[] { new Claim(ClaimTypes.NameIdentifier, userId), new Claim(ClaimTypes.Name, username), new Claim(ClaimTypes.Email, email) }), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(symmetricKey), SecurityAlgorithms.HmacSha256Signature) }; SecurityToken stoken = tokenHandler.CreateToken(tokenDescriptor); string token = tokenHandler.WriteToken(stoken); return(token); } catch (Exception exception) { ServiceHelper.LogException(exception, new Dictionary <string, string>(), ErrorSource.Security); return(string.Empty); } }
public ActionResult VerifyEmail(string guidCode) { try { ActionResult actionResult = ServiceHelper.IsAnyNullOrEmpty(guidCode); if (actionResult.Success) { actionResult.Success = false; return(actionResult); } VerificationToken verification = UserManagement.GetVerificationToken(guidCode); ActionResult result = UserManagement.VerifyToken(verification.UserEmail, verification.UserToken); Uri requestUrl = new Uri(HttpContext.Current.Request.Url, "/index.html?" + result.Success); HttpContext.Current.Response.Redirect(requestUrl.ToString()); return(result); } catch (Exception exception) { Dictionary <string, string> dictionary = guidCode.ToDictionary(); ServiceHelper.LogException(exception, dictionary, ErrorSource.Authentication); return(new ActionResult { Success = false, Message = "Error, failed to verify email." }); } }
public List <ProductMediaResponse> GetProductMedia(string productId) { try { using (ESamhashoEntities entities = new ESamhashoEntities()) { if (long.TryParse(productId, out long id)) { List <ProductMediaResponse> productResponses = entities.ProductMedias.Where(a => a.ProductId == id && !a.IsDeleted) .Select(a => new ProductMediaResponse { MediaSource = a.MediaSource, Id = a.Id }).ToList(); return(productResponses); } return(new List <ProductMediaResponse>()); } } catch (Exception exception) { Dictionary <string, string> dictionary = new Dictionary <string, string> { { "productId", productId } }; ServiceHelper.LogException(exception, dictionary, ErrorSource.Product); return(new List <ProductMediaResponse>()); } }
public static ClaimsPrincipal GetPrincipal(string token) { try { JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler(); JwtSecurityToken jwtToken = tokenHandler.ReadToken(token) as JwtSecurityToken; if (jwtToken == null) { return(null); } byte[] symmetricKey = Convert.FromBase64String(key); TokenValidationParameters validationParameters = new TokenValidationParameters() { RequireExpirationTime = true, ValidateIssuer = false, ValidateAudience = false, IssuerSigningKey = new SymmetricSecurityKey(symmetricKey) }; SecurityToken securityToken; ClaimsPrincipal principal = tokenHandler.ValidateToken(token, validationParameters, out securityToken); return(principal); } catch (Exception exception) { ServiceHelper.LogException(exception, new Dictionary <string, string>(), ErrorSource.Security); return(null); } }
public List <ProductResponse> GetAllProducts() { try { using (ESamhashoEntities entities = new ESamhashoEntities()) { List <ProductResponse> productResponses = entities.GetAllProducts().Select(a => new ProductResponse { IsDeleted = a.IsDeleted, Description = a.Description, Code = a.Code, ModifiedDate = a.ModifiedDate.ToLongDateString(), Name = a.Name, Manufacturer = a.Manufacturer, UserId = a.UserId, MediaSource = a.MediaSource, ProductId = a.ProductId, Price = a.Price, Per = a.Per, ShortDescription = a.ShortDescription, CreatedDate = a.CreatedDate.ToLongDateString() + " " + a.CreatedDate.ToLongTimeString(), CatergoryId = a.CatergoryId, Catergory = a.Catergory, IsMain = a.IsMain, IsActive = a.IsActive }).ToList(); return(productResponses); } } catch (Exception exception) { ServiceHelper.LogException(exception, new Dictionary <string, string>(), ErrorSource.Product); return(new List <ProductResponse>()); } }
public List <BlogResponse> GetBlogs() { try { using (ESamhashoEntities entities = new ESamhashoEntities()) { List <Blog> responses = entities.Blogs.Where(a => !a.IsDeleted).OrderByDescending(a => a.DateCreated).ToList(); List <BlogResponse> blogs = responses.Select(a => new BlogResponse { Name = a.Name, DateCreated = a.DateCreated.ToLongDateString() + " " + a.DateCreated.ToLongTimeString(), Description = a.Description, Id = a.Id, TitleDescription = a.TitleDescription, MediaUrl = a.MediaUrl }).ToList(); return(blogs); } } catch (Exception exception) { ServiceHelper.LogException(exception, new Dictionary <string, string>(), ErrorSource.Blog); return(new List <BlogResponse>()); } }
public ActionResult DeleteProductMedia(string mediaId) { try { using (ESamhashoEntities entities = new ESamhashoEntities()) { if (!long.TryParse(mediaId, out long id)) { return new ActionResult { Message = "Deleted failed" } } ; entities.DeleteProductMedia(id); return(new ActionResult { Message = "Deleted successfully", Success = true }); } } catch (Exception exception) { Dictionary <string, string> dictionary = new Dictionary <string, string> { { "mediaId", mediaId } }; ServiceHelper.LogException(exception, dictionary, ErrorSource.Product); return(new ActionResult { Message = "Error, failed to delete product media, try again." }); } }
public ActionResult AddNotification(NotificationData notificationRequest) { try { using (ESamhashoEntities entities = new ESamhashoEntities()) { Notification notification = new Notification { IsDeleted = notificationRequest.IsDeleted, DateCreated = DateTime.Now, FromCustomer = notificationRequest.FromCustomer, NotificationTypeId = notificationRequest.NotificationTypeId, Email = notificationRequest.Email, IsRead = notificationRequest.IsRead, Message = notificationRequest.Message, Name = notificationRequest.Name, Phone = notificationRequest.Phone, ProductId = notificationRequest.ProductId == 0?null: notificationRequest.ProductId }; entities.Notifications.Add(notification); entities.SaveChanges(); return(new ActionResult { Success = true, Message = "Notification saved successfully" }); } } catch (Exception exception) { Dictionary <string, string> dictionary = notificationRequest.ToDictionary(); ServiceHelper.LogException(exception, dictionary, ErrorSource.Notification); return(new ActionResult { Message = "Error, notification failed to save, try again." }); } }
public ActionResult UpdateProduct(ProductRequest product) { try { using (ESamhashoEntities entities = new ESamhashoEntities()) { if (product.IsActive) { List <Product> products = entities.Products.ToList(); products.ForEach(a => a.IsActive = false); entities.SaveChanges(); } if (product.IsMain) { List <Product> products = entities.Products.Where(a => a.IsMain).ToList(); if (products.Count > 3) { Product productMain = products.FirstOrDefault(); if (productMain != null) { productMain.IsMain = false; } entities.SaveChanges(); } } Product updateProduct = entities.Products.FirstOrDefault(a => a.Id == product.ProductId); if (updateProduct != null) { updateProduct.Description = product.Description; updateProduct.ShortDescription = product.ShortDescription; updateProduct.CatergoryId = product.Catergory; updateProduct.Price = product.Price; updateProduct.Per = product.Per; updateProduct.Manufacturer = product.Manufacturer; updateProduct.IsActive = product.IsActive; updateProduct.IsMain = product.IsMain; updateProduct.Name = product.Name; } entities.SaveChanges(); return(new ActionResult { Message = "Product updated successfully.", Success = true }); } } catch (Exception exception) { Dictionary <string, string> dictionary = product.ToDictionary(); ServiceHelper.LogException(exception, dictionary, ErrorSource.Product); return(new ActionResult { Message = "Error, failed to update product, try again." }); } }
public static VerificationToken GetVerificationToken(string guidCode) { try { ESamhashoEntities entities = new ESamhashoEntities(); return(entities.VerificationTokens.FirstOrDefault(a => a.Id.Equals(guidCode))); } catch (Exception exception) { Dictionary <string, string> dictionary = guidCode.ToDictionary(); ServiceHelper.LogException(exception, dictionary, ErrorSource.UserManagement); return(new VerificationToken()); } }
public ActionResult LockUser(string username) { try { ActionResult actionResult = ServiceHelper.IsAnyNullOrEmpty(username); if (actionResult.Success) { actionResult.Success = false; return(actionResult); } ApplicationUserManager manager = new ApplicationUserManager(new UserStore <ApplicationUser>(new ApplicationDbContext())); ApplicationUser user = manager.FindByName(username); if (user == null) { return(new ActionResult { Message = "User does not exist", Success = false }); } IdentityResult result = manager.SetLockoutEnabled(user.Id, true); if (result.Succeeded) { return(new ActionResult { Message = "User successfully locked.", Success = true }); } string error = result.Errors.Aggregate(string.Empty, (current, resultError) => current + resultError + Environment.NewLine); return(new ActionResult { Message = error }); } catch (Exception exception) { Dictionary <string, string> dictionary = username.ToDictionary(); ServiceHelper.LogException(exception, dictionary, ErrorSource.Authentication); return(new ActionResult { Success = false, Message = "Error, failed to lock user." }); } }
public BlogResponse AddBlog(Stream blogData) { MultipartFormDataParser dataParser = new MultipartFormDataParser(blogData); try { string name = dataParser.GetParameterValue("name"); string description = dataParser.GetParameterValue("description"); string titleDescription = dataParser.GetParameterValue("titleDescription"); FilePart dataParserFile = dataParser.Files[0]; string path = ServiceHelper.SaveImage(dataParserFile.Data, dataParserFile.FileName); using (ESamhashoEntities entities = new ESamhashoEntities()) { Blog blog = new Blog { Name = name, IsDeleted = false, Description = description, DateCreated = DateTime.Now, MediaUrl = path, TitleDescription = titleDescription }; entities.Blogs.Add(blog); entities.SaveChanges(); return(new BlogResponse { Name = blog.Name, Description = blog.Description, TitleDescription = blog.TitleDescription, Id = blog.Id, DateCreated = blog.DateCreated.ToLongDateString() + " " + blog.DateCreated.ToLongTimeString(), MediaUrl = blog.MediaUrl }); } } catch (Exception exception) { Dictionary <string, string> dictionary = new Dictionary <string, string> { { "name", dataParser.GetParameterValue("name") }, { "description", dataParser.GetParameterValue("description") }, { "titleDescription", dataParser.GetParameterValue("titleDescription") } }; ServiceHelper.LogException(exception, dictionary, ErrorSource.Blog); return(new BlogResponse()); } }
public ProductResponse GetProduct(string productId) { try { using (ESamhashoEntities entities = new ESamhashoEntities()) { if (long.TryParse(productId, out long id)) { ProductResponse productResponses = entities.GetProduct(id) .Select( a => new ProductResponse { IsDeleted = a.IsDeleted, Description = a.Description, Code = a.Code, ModifiedDate = a.ModifiedDate.ToLongDateString(), Name = a.Name, Manufacturer = a.Manufacturer, UserId = a.UserId, MediaSource = a.MediaSource, ProductId = a.ProductId, Price = a.Price, Per = a.Per, ShortDescription = a.ShortDescription, CreatedDate = a.CreatedDate.ToLongDateString() + " " + a.CreatedDate.ToLongTimeString(), CatergoryId = a.CatergoryId, Catergory = a.Catergory, IsMain = a.IsMain, IsActive = a.IsActive }) .FirstOrDefault(); return(productResponses); } return(new ProductResponse()); } } catch (Exception exception) { Dictionary <string, string> dictionary = new Dictionary <string, string> { { "productId", productId } }; ServiceHelper.LogException(exception, dictionary, ErrorSource.Product); return(new ProductResponse()); } }
public ActionResult AddBlogViewer(Viewer request) { try { using (ESamhashoEntities entities = new ESamhashoEntities()) { DateTime dateTime = DateTime.Now.AddDays(-1); BlogViewer viewer = entities.BlogViewers.FirstOrDefault( a => a.BlogId == request.DataId && a.IpAddress.Equals(request.IpAddress) && a.DateCreated >= dateTime && a.DateCreated <= DateTime.Now); if (viewer != null) { return(new ActionResult { Success = true, Message = "" }); } BlogViewer blogViewer = new BlogViewer { DateCreated = DateTime.Now, BlogId = (int)request.DataId, Country = request.Country, IpAddress = request.IpAddress, Town = request.Town }; entities.BlogViewers.Add(blogViewer); entities.SaveChanges(); return(new ActionResult { Success = true, Message = "" }); } } catch (Exception exception) { Dictionary <string, string> dictionary = request.ToDictionary(); ServiceHelper.LogException(exception, dictionary, ErrorSource.Blog); return(new ActionResult { Message = "Error, blog failed to confirm viewed, error." }); } }
public List <ViewerResponse> GetBlogViews() { try { using (ESamhashoEntities entities = new ESamhashoEntities()) { List <BlogViewer> blogViews = entities.BlogViewers.ToList(); return((from blogViewer in blogViews let blog = entities.Blogs.FirstOrDefault(a => a.Id == blogViewer.BlogId) where blog != null select new ViewerResponse { ViewId = blogViewer.Id, Country = blogViewer.Country, Name = blog.Name, IpAddress = blogViewer.IpAddress, Town = blogViewer.Town, DateCreated = blogViewer.DateCreated }).ToList()); } } catch (Exception exception) { ServiceHelper.LogException(exception, new Dictionary <string, string>(), ErrorSource.Blog); return(new List <ViewerResponse> { new ViewerResponse() }); } }
public List <ViewerResponse> GetProductViews() { try { using (ESamhashoEntities entities = new ESamhashoEntities()) { List <ProductViewer> productViews = entities.ProductViewers.ToList(); return((from productViewer in productViews let product = entities.Products.FirstOrDefault(a => a.Id == productViewer.ProductId) where product != null select new ViewerResponse { ViewId = productViewer.Id, Country = productViewer.Country, Name = product.Name, IpAddress = productViewer.IpAddress, Town = productViewer.Town, DateCreated = productViewer.DateCreated }).ToList()); } } catch (Exception exception) { ServiceHelper.LogException(exception, new Dictionary <string, string>(), ErrorSource.Product); return(new List <ViewerResponse> { new ViewerResponse() }); } }
public List <CatergoryResponse> GetAllCatergories() { try { using (ESamhashoEntities entities = new ESamhashoEntities()) { List <CatergoryResponse> responses = entities.Catergories.Where(a => !a.IsDeleted).OrderBy(a => a.Name).Select(a => new CatergoryResponse { Name = a.Name, Id = a.Id }).ToList(); return(responses); } } catch (Exception exception) { ServiceHelper.LogException(exception, new Dictionary <string, string>(), ErrorSource.Catergory); return(new List <CatergoryResponse>()); } }
public List <NotificationData> GetNotificationsByProduct(string productId) { try { using (ESamhashoEntities entities = new ESamhashoEntities()) { if (!long.TryParse(productId, out long id)) { return(new List <NotificationData>()); } List <Notification> notifications = entities.Notifications .Where(a => !a.IsDeleted && a.ProductId == id).OrderByDescending(a => a.DateCreated).ToList(); List <NotificationData> notificationDatas = notifications .Select( a => new NotificationData { Message = a.Message, IsDeleted = a.IsDeleted, Name = a.Name, ProductId = a.ProductId, IsRead = a.IsRead, Phone = a.Phone, DateCreated = a.DateCreated.ToLongDateString() + " " + a.DateCreated .ToLongTimeString(), Email = a.Email, Id = a.Id }).ToList(); return(notificationDatas); } } catch (Exception exception) { Dictionary <string, string> dictionary = new Dictionary <string, string> { { "productId", productId } }; ServiceHelper.LogException(exception, dictionary, ErrorSource.Notification); return(new List <NotificationData>()); } }
public ActionResult DeleteBlog(string blogId) { try { if (!int.TryParse(blogId, out int id)) { return new ActionResult { Message = "Blog is invalid" } } ; using (ESamhashoEntities entities = new ESamhashoEntities()) { Blog blog = entities.Blogs.FirstOrDefault(a => a.Id == id); if (blog == null) { return new ActionResult { Message = "Blog does not exist" } } ; blog.IsDeleted = true; entities.SaveChanges(); return(new ActionResult { Success = true, Message = "Blog deleted successfully" }); } } catch (Exception exception) { Dictionary <string, string> dictionary = new Dictionary <string, string> { { "blogId", blogId } }; ServiceHelper.LogException(exception, dictionary, ErrorSource.Blog); return(new ActionResult { Message = "Error, blog failed to delete, try again." }); } }
public ProductMediaResponse AddProductMedia(Stream media) { MultipartFormDataParser dataParser = new MultipartFormDataParser(media); string productId = dataParser.GetParameterValue("productId"); try { using (ESamhashoEntities entities = new ESamhashoEntities()) { string path = ServiceHelper.SaveImage(dataParser.Files[0].Data, dataParser.Files[0].FileName); if (long.TryParse(productId, out long id)) { ProductMedia productMedia = new ProductMedia { MediaSource = path, ProductId = id, DateCreate = DateTime.Now, IsDeleted = false, IsMain = false }; entities.ProductMedias.Add(productMedia); entities.SaveChanges(); return(new ProductMediaResponse { MediaSource = path, Id = productMedia.Id }); } return(new ProductMediaResponse()); } } catch (Exception exception) { Dictionary <string, string> dictionary = new Dictionary <string, string> { { "productId", productId } }; ServiceHelper.LogException(exception, dictionary, ErrorSource.Product); return(new ProductMediaResponse()); } }
public UserProfileDetails GetUserDetails(Token token) { try { using (ESamhashoEntities entities = new ESamhashoEntities()) { UserProfile userDetails = entities.UserProfiles.FirstOrDefault(); if (userDetails != null) { ApplicationUserManager userManager = HttpContext.Current.GetOwinContext().GetUserManager <ApplicationUserManager>(); ApplicationUser aspUser = userManager.FindById(userDetails.UserId); UserProfileDetails user = new UserProfileDetails { DateCreated = userDetails.DateCreated.ToLongDateString(), Id = userDetails.Id, UserId = userDetails.UserId, City = userDetails.City, AboutMe = userDetails.AboutMe, Address = userDetails.Address, FirstName = userDetails.FirstName, LastName = userDetails.LastName, ProfilePicture = userDetails.ProfilePicture, Country = userDetails.Country, UserName = aspUser.UserName, Email = aspUser.Email, PhoneNumber = aspUser.PhoneNumber }; return(user); } return(new UserProfileDetails()); } } catch (Exception exception) { Dictionary <string, string> dictionary = token.ToDictionary(); ServiceHelper.LogException(exception, dictionary, ErrorSource.User); return(new UserProfileDetails()); } }
public ActionResult ReadNotification(string notificationId) { try { using (ESamhashoEntities entities = new ESamhashoEntities()) { if (!long.TryParse(notificationId, out long id)) { return(new ActionResult { Message = "Notification is invalid" }); } Notification notification = entities.Notifications.FirstOrDefault(a => a.Id == id); if (notification == null) { return new ActionResult { Message = "Notification does not exist" } } ; notification.IsRead = true; entities.SaveChanges(); return(new ActionResult { Success = true, Message = "Notification saved successfully" }); } } catch (Exception exception) { Dictionary <string, string> dictionary = new Dictionary <string, string> { { "notificationId", notificationId } }; ServiceHelper.LogException(exception, dictionary, ErrorSource.Notification); return(new ActionResult { Message = "Error, failed to mark notification as read" }); } }
public ActionResult DeleteCatergory(string productId) { try { using (ESamhashoEntities entities = new ESamhashoEntities()) { if (!long.TryParse(productId, out long id)) { return(new ActionResult { Message = "Catergory is invalid." }); } Catergory catergory = entities.Catergories.FirstOrDefault(a => a.Id == id); if (catergory == null) { return new ActionResult { Message = "Catergory does not exist." } } ; catergory.IsDeleted = true; entities.SaveChanges(); return(new ActionResult { Success = true, Message = "Catergory saved successfully." }); } } catch (Exception exception) { Dictionary <string, string> dictionary = new Dictionary <string, string> { { "productId", productId } }; ServiceHelper.LogException(exception, dictionary, ErrorSource.Catergory); return(new ActionResult { Message = "Error, catergory failed to delete, try again." }); } }
public static bool ValidateToken(string token, out List <Claim> claims) { try { claims = new List <Claim>(); ClaimsPrincipal simplePrinciple = GetPrincipal(token); ClaimsIdentity identity = simplePrinciple.Identity as ClaimsIdentity; if (identity == null) { return(false); } if (!identity.IsAuthenticated) { return(false); } Claim userIdClaim = identity.FindFirst(ClaimTypes.NameIdentifier); Claim nameClaim = identity.FindFirst(ClaimTypes.Name); Claim emailClaim = identity.FindFirst(ClaimTypes.Email); claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, userIdClaim.Value), new Claim(ClaimTypes.Email, emailClaim.Value), new Claim(ClaimTypes.Name, nameClaim.Value) }; return(!string.IsNullOrEmpty(userIdClaim.Value)); } catch (Exception exception) { ServiceHelper.LogException(exception, new Dictionary <string, string>(), ErrorSource.Authentication); claims = new List <Claim>(); return(false); } }
public ActionResult UpdateUserDetails(Stream userData) { MultipartFormDataParser dataParser = new MultipartFormDataParser(userData); try { string aboutMe = dataParser.GetParameterValue("aboutMe"); string address = dataParser.GetParameterValue("address"); string city = dataParser.GetParameterValue("city"); string country = dataParser.GetParameterValue("country"); string firstName = dataParser.GetParameterValue("firstName"); string lastName = dataParser.GetParameterValue("lastName"); string token = dataParser.GetParameterValue("token"); string phoneNumber = dataParser.GetParameterValue("phonenumber"); string email = dataParser.GetParameterValue("email"); AuthenticationService authenticationService = new AuthenticationService(); IPrincipal jwtToken = authenticationService.AuthenticateJwtToken(token); string userId = jwtToken.Identity.GetUserId(); string path = null; if (dataParser.Files.Any()) { FilePart dataParserFile = dataParser.Files[0]; path = ServiceHelper.SaveImage(dataParserFile.Data, dataParserFile.FileName); } using (ESamhashoEntities entities = new ESamhashoEntities()) { AspNetUser netUser = entities.AspNetUsers.FirstOrDefault(a => a.Id.Equals(userId)); if (netUser != null) { netUser.Email = String.IsNullOrEmpty(email) ? netUser.Email : email; netUser.PhoneNumber = String.IsNullOrEmpty(phoneNumber) ? netUser.PhoneNumber : phoneNumber; } UserProfile userProfile = entities.UserProfiles.FirstOrDefault(a => a.UserId.Equals(userId)); if (userProfile != null) { userProfile.AboutMe = String.IsNullOrEmpty(aboutMe) ? userProfile.AboutMe : aboutMe; userProfile.Address = String.IsNullOrEmpty(address) ? userProfile.Address : address; userProfile.City = String.IsNullOrEmpty(city) ? userProfile.City : city; userProfile.Country = String.IsNullOrEmpty(country) ? userProfile.Country : country; userProfile.FirstName = String.IsNullOrEmpty(firstName) ? userProfile.FirstName : firstName; userProfile.LastName = String.IsNullOrEmpty(lastName) ? userProfile.LastName : lastName; userProfile.ProfilePicture = String.IsNullOrEmpty(path)?userProfile.ProfilePicture:path; userProfile.UserId = userId; } else { UserProfile addUserProfile = new UserProfile { DateCreated = DateTime.Now, AboutMe = aboutMe, Address = address, City = city, Country = country, FirstName = firstName, LastName = lastName, ProfilePicture = path, UserId = userId }; entities.UserProfiles.Add(addUserProfile); } entities.SaveChanges(); return(new ActionResult { Message = path, Success = true }); } } catch (Exception exception) { Dictionary <string, string> dictionary = new Dictionary <string, string> { { "aboutMe", dataParser.GetParameterValue("aboutMe") }, { "address", dataParser.GetParameterValue("address") }, { "city", dataParser.GetParameterValue("city") }, { "country", dataParser.GetParameterValue("country") }, { "firstName", dataParser.GetParameterValue("firstName") }, { "lastName", dataParser.GetParameterValue("lastName") }, { "userId", dataParser.GetParameterValue("userId") } }; ServiceHelper.LogException(exception, dictionary, ErrorSource.User); return(new ActionResult { Message = "Failed to save profile, try again.", Success = true }); } }