Beispiel #1
0
        public async Task <IActionResult> EditMusic([FromForm] EditMusicModel model)
        {
            string musicFileName, imageFileName;
            User   user = await db.Users.FindAsync(UserId);

            Music music = await db.Musics.FindAsync(model.Id);

            string dateTimeNow = $"{DateTime.Now.Day}.{DateTime.Now.Month}.{DateTime.Now.Year} {DateTime.Now.Hour}:{DateTime.Now.Minute}:{DateTime.Now.Second}";

            try
            {
                if (model.MusicFile != null)
                {
                    musicFileName  = $"{user.Login}_{dateTimeNow}_" + model.MusicFile.FileName;
                    music.MusicUrl = await cloud.EditFile("", music.MusicFileName, "", musicFileName, model.MusicFile.OpenReadStream());

                    music.MusicFileName = musicFileName;
                }
                if (model.MusicImageFile != null)
                {
                    imageFileName = $"{user.Login}_music_{dateTimeNow}_" + model.MusicImageFile.FileName;
                    if (music.MusicImageName != "default.png")
                    {
                        music.MusicImageUrl = await cloud.EditFile("", music.MusicImageName, "", imageFileName, model.MusicImageFile.OpenReadStream());

                        music.MusicImageName = imageFileName;
                    }
                    else
                    {
                        music.MusicImageUrl = await cloud.AddFile("", imageFileName, model.MusicImageFile.OpenReadStream());

                        music.MusicImageName = imageFileName;
                    }
                }
                music.MusicName    = model.MusicName;
                music.MusicGenreId = model.MusicGenreId;
                await db.SaveChangesAsync();

                return(Ok(new { msg = "" }));
            }
            catch
            {
                return(StatusCode(500));
            }
        }