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 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 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 }); } }
public CreateProductResponse CreateProduct(Stream data) { MultipartFormDataParser dataParser = new MultipartFormDataParser(data); string name = dataParser.GetParameterValue("name"); string description = dataParser.GetParameterValue("description"); string catergoryId = dataParser.GetParameterValue("catergory"); string price = dataParser.GetParameterValue("price"); string per = dataParser.GetParameterValue("per"); string shortDescription = dataParser.GetParameterValue("shortDescription"); string manufacturer = dataParser.GetParameterValue("manufacturer"); string code = dataParser.GetParameterValue("code"); string userId = dataParser.GetParameterValue("userId"); string isMain = dataParser.GetParameterValue("IsMain"); string isActive = dataParser.GetParameterValue("isActive"); try { bool.TryParse(isMain, out bool checkMain); bool.TryParse(isActive, out bool checkActive); using (ESamhashoEntities entities = new ESamhashoEntities()) { if (checkActive) { List <Product> products = entities.Products.ToList(); products.ForEach(a => a.IsActive = false); entities.SaveChanges(); } if (checkMain) { 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 product = new Product { CatergoryId = int.Parse(catergoryId), Code = code, CreatedDate = DateTime.Now, Description = description, IsDeleted = false, Manufacturer = manufacturer, ModifiedDate = DateTime.Now, Name = name, Per = per, Price = decimal.Parse(price), ShortDescription = shortDescription, UserId = userId, IsMain = checkMain, IsActive = checkActive }; entities.Products.Add(product); entities.SaveChanges(); string path = ServiceHelper.SaveImage(dataParser.Files[0].Data, dataParser.Files[0].FileName); ProductMedia productMedia = new ProductMedia { MediaSource = path, ProductId = product.Id, DateCreate = DateTime.Now, IsDeleted = false, IsMain = true }; entities.ProductMedias.Add(productMedia); entities.SaveChanges(); Catergory catergory = entities.Catergories.FirstOrDefault(a => a.Id == product.CatergoryId); return(new CreateProductResponse { DateCreated = product.CreatedDate.ToLongDateString() + " " + product.CreatedDate.ToLongTimeString(), ProductId = product.Id, MediaSource = path, Catergory = catergory == null? "":catergory.Name }); } } catch (Exception exception) { Dictionary <string, string> dictionary = new Dictionary <string, string> { { "name", name }, { "description", description }, { "catergoryId", catergoryId }, { "price", price }, { "per", per }, { "shortDescription", shortDescription }, { "manufacturer", manufacturer }, { "code", code }, { "userId", userId }, { "mainImage", true.ToString() }, }; ServiceHelper.LogException(exception, dictionary, ErrorSource.Product); return(new CreateProductResponse()); } }