Exemple #1
0
        ArtistPage SaveRemoteArtistToDB(string artistJson)
        {
            if (string.IsNullOrEmpty(artistJson))
            {
                return(null);
            }

            var        artist     = (JObject)JsonConvert.DeserializeObject(artistJson);
            ArtistPage artistPage = new ArtistPage()
            {
                PageOwnerId      = _workContext.CurrentCustomer.IsAdmin() ? _workContext.CurrentCustomer.Id : 0,
                Biography        = artist["Description"].ToString(),
                Name             = artist["Name"].ToString(),
                Gender           = artist["Gender"].ToString(),
                HomeTown         = artist["HomeTown"].ToString(),
                RemoteEntityId   = artist["RemoteEntityId"].ToString(),
                RemoteSourceName = artist["RemoteSourceName"].ToString(),
                ShortDescription = "",
            };

            _artistPageService.Insert(artistPage);

            //we can now download the image from the server and store it on our own server
            //use the json we retrieved earlier

            if (!string.IsNullOrEmpty(artist["ImageUrl"].ToString()))
            {
                var imageUrl   = artist["ImageUrl"].ToString();
                var imageBytes = HttpHelper.ExecuteGET(imageUrl);
                if (imageBytes != null)
                {
                    var fileExtension = Path.GetExtension(imageUrl);
                    if (!String.IsNullOrEmpty(fileExtension))
                    {
                        fileExtension = fileExtension.ToLowerInvariant();
                    }

                    var contentType = PictureUtility.GetContentType(fileExtension);

                    var picture       = _pictureService.InsertPicture(imageBytes, contentType, artistPage.GetSeName(_workContext.WorkingLanguage.Id, true, false));
                    var artistPicture = new ArtistPagePicture()
                    {
                        EntityId     = artistPage.Id,
                        DateCreated  = DateTime.Now,
                        DateUpdated  = DateTime.Now,
                        DisplayOrder = 1,
                        PictureId    = picture.Id
                    };
                    _artistPageService.InsertPicture(artistPicture);
                }
            }
            return(artistPage);
        }
        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"
                }));
            }
        }