Esempio n. 1
0
        public async Task <IActionResult> CreateArtist(CreateArtistModel model)
        {
            if (ModelState.IsValid)
            {
                ArtistPicture artistPicture = new ArtistPicture();

                using (var memoryStream = new MemoryStream())
                {
                    await model.PictureOne.CopyToAsync(memoryStream);

                    artistPicture.PictureOne = memoryStream.ToArray();
                    if (model.PictureTwo != null)
                    {
                        await model.PictureTwo.CopyToAsync(memoryStream);

                        artistPicture.PictureTwo = memoryStream.ToArray();
                    }
                }

                bool result = await repository.CreateArtistPicAsync(artistPicture);

                if (result)
                {
                    Artist artist = new Artist
                    {
                        ArtistName      = model.ArtistName,
                        ArtistBio       = model.ArtistBio,
                        ArtistPictureId = artistPicture.ArtistPictureId
                    };
                    result = await repository.CreateArtistAsync(artist);

                    if (result)
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                    else
                    {
                        ModelState.AddModelError("", "An error occured while creating the Artist");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "An error occured while creating the adding the Artist pictures");
                }
            }
            return(View(model));
        }