public IHttpActionResult Post(EntityPropertyModel requestModel) { var entityName = requestModel.EntityName; var propertyName = requestModel.PropertyName; var propertyValue = requestModel.Value; var entityId = requestModel.EntityId; var currentUser = ApplicationContext.Current.CurrentUser; Media media = null; if (PropertyNames.IsMediaPropertyName(propertyName)) { //the property value must be an integer var valueAsInteger = propertyValue.GetInteger(false); if (valueAsInteger == 0) { return(BadRequest()); } //get media since this is media property, let's get the media first media = _mediaService.Get(valueAsInteger); //is the person trying to mess around actually is a capable person if (!currentUser.CanEditResource(media)) { return(RespondFailure("Unauthorized", "post_entityproperty")); } } //get valid system property name if available propertyName = PropertyNames.ParseToValidSystemPropertyName(propertyName) ?? propertyName; switch (entityName.ToLower()) { case "user": //somebody is trying to set the user's properties. He must be the user himself or administrator var user = _userService.Get(entityId); if (user == null || !currentUser.CanEditUser(user)) { return(NotFound()); } user.SetPropertyValue(propertyName, propertyValue); if (media != null) { //also attach the media to user so we can show them all at one place //this way if user wants to delete the media, we'll simply detach them immediately to postpone till a service performs deletion _mediaService.AttachMediaToEntity(user, media); } break; } return(RespondSuccess()); }
public IHttpActionResult Post(UserSkillEntityModel model) { if (!ModelState.IsValid) { return(BadRequest()); } var currentUser = ApplicationContext.Current.CurrentUser; //if it's admin, we can safely change the customer id otherwise we'll save skill as logged in user var isAdmin = currentUser.IsAdministrator(); if (!isAdmin && model.UserId > 0) { model.UserId = currentUser.Id; } if (model.SystemSkill && isAdmin) { model.UserId = 0; } else { model.UserId = currentUser.Id; } var mediaIds = model.MediaId?.ToList() ?? new List <int>(); //get all medias var medias = _mediaService.Get(x => mediaIds.Contains(x.Id) && x.UserId == currentUser.Id).ToList(); //get skill, 1.) by id 2.) by name 3.) create new otherwise var skill = _skillService.Get(model.Id) ?? (_skillService.FirstOrDefault(x => x.Name == model.SkillName) ?? new Skill() { DisplayOrder = model.DisplayOrder, UserId = currentUser.Id, Name = model.SkillName }); //should we add this? if (skill.Id == 0) { _skillService.Insert(skill); } else { if (model.SystemSkill && isAdmin) { skill.Name = model.SkillName; _skillService.Update(skill); } } //if user id is not 0, attach this skill with user if (model.UserId != 0) { var userSkill = model.UserSkillId > 0 ? _userSkillService.Get(model.UserSkillId) : new UserSkill() { UserId = model.UserId, SkillId = skill.Id, Description = model.Description, DisplayOrder = model.DisplayOrder, ExternalUrl = model.ExternalUrl }; if (userSkill.Id == 0) { _userSkillService.Insert(userSkill); } else { _userSkillService.Update(userSkill); } //attach media if it exists foreach (var media in medias) { _mediaService.AttachMediaToEntity(userSkill, media); } return(RespondSuccess(new { Skill = userSkill.ToModel(_mediaService, _mediaSettings, _generalSettings) })); } return(RespondSuccess(new { Skill = skill.ToModel() })); }
public IHttpActionResult SaveArtist(ArtistPageModel model) { if (!ModelState.IsValid) { VerboseReporter.ReportError("Invalid data submitted. Please check all fields and try again.", "save_artist"); return(RespondFailure()); } if (!ApplicationContext.Current.CurrentUser.IsRegistered()) { VerboseReporter.ReportError("Unauthorized access", "save_artist"); return(RespondFailure()); } //check to see if artist name already exists string artistJson; if (IsArtistPageNameAvailable(model.Name, out artistJson)) { var artistPage = new ArtistPage() { PageOwnerId = ApplicationContext.Current.CurrentUser.Id, Biography = model.Description, Name = model.Name, DateOfBirth = model.DateOfBirth, Gender = model.Gender, HomeTown = model.HomeTown, RemoteEntityId = model.RemoteEntityId, RemoteSourceName = model.RemoteSourceName, ShortDescription = model.ShortDescription }; _artistPageService.Insert(artistPage); if (artistJson != "") { //we can now download the image from the server and store it on our own server //use the json we retrieved earlier var jObject = (JObject)JsonConvert.DeserializeObject(artistJson); if (!string.IsNullOrEmpty(jObject["ImageUrl"].ToString())) { var imageUrl = jObject["ImageUrl"].ToString(); var imageBytes = HttpHelper.ExecuteGet(imageUrl); var fileExtension = Path.GetExtension(imageUrl); if (!string.IsNullOrEmpty(fileExtension)) { fileExtension = fileExtension.ToLowerInvariant(); } var contentType = PictureUtility.GetContentType(fileExtension); var picture = new Media() { Binary = imageBytes, Name = model.Name, MimeType = contentType }; _pictureService.WritePictureBytes(picture, _mediaSettings.PictureSaveLocation); //relate both page and picture _pictureService.AttachMediaToEntity(artistPage, picture); } } return(Response(new { Success = true, RedirectTo = Url.Route("ArtistPageUrl", new RouteValueDictionary() { { "SeName", artistPage.GetPermalink() } }) })); } else { return(Response(new { Success = false, Message = "DuplicateName" })); } }
public IHttpActionResult Post(UserSkillEntityModel model) { if (!ModelState.IsValid) { return(BadRequest()); } var currentUser = _workContext.CurrentCustomer; //if it's admin, we can safely change the customer id otherwise we'll save skill as logged in user var isAdmin = currentUser.IsAdmin(); if (!isAdmin && model.UserId > 0) { model.UserId = currentUser.Id; } if (model.SystemSkill && isAdmin) { model.UserId = 0; } else { model.UserId = currentUser.Id; } var mediaIds = model.MediaId?.ToList() ?? new List <int>(); //get all medias var medias = _mediaService.Get(x => mediaIds.Contains(x.Id) && x.UserId == currentUser.Id).ToList(); //get skill, 1.) by id 2.) by name 3.) create new otherwise var skill = _skillService.GetById(model.Id) ?? (_skillService.FirstOrDefault(x => x.Name == model.SkillName) ?? new Skill() { DisplayOrder = model.DisplayOrder, UserId = currentUser.Id, Name = model.SkillName, Description = model.Description }); //should we add this? if (skill.Id == 0) { _skillService.Insert(skill); } else { if (model.SystemSkill && isAdmin) { skill.Name = model.SkillName; skill.Description = model.Description; _skillService.Update(skill); } } //if user id is not 0, attach this skill with user if (model.UserId != 0) { //but first check if this skill has already been assigned to user or not if (model.UserSkillId == 0) { var savedSkill = _userSkillService.FirstOrDefault(x => x.SkillId == skill.Id); if (savedSkill != null) { //the skill is already part of user, we won't add it again return(Response(new { Success = false, Message = "The skill has already been added to this profile" })); } } var userSkill = model.UserSkillId > 0 ? _userSkillService.GetById(model.UserSkillId) : new UserSkill() { UserId = model.UserId, SkillId = skill.Id, Description = model.Description, DisplayOrder = model.DisplayOrder, ExternalUrl = model.ExternalUrl }; if (userSkill.Id == 0) { _userSkillService.Insert(userSkill); } else { _userSkillService.Update(userSkill); } //attach media if it exists foreach (var media in medias) { _mediaService.AttachMediaToEntity(userSkill, media); } return(Response(new { Success = true, Skill = userSkill.ToModel(_mediaService, _mediaSettings, _workContext, _storeContext, _userService, _customerProfileViewService, _customerProfileService, _pictureService, Url, _webHelper) })); } return(Response(new { Success = true, Skill = skill.ToModel(_workContext) })); }