コード例 #1
0
ファイル: MusicController.cs プロジェクト: EladHeller/passkol
        public ActionResult SaveMusic(string editedMusic, HttpPostedFileBase WAVFile, HttpPostedFileBase MP3File, string tagIdsStr)
        {
            ResponseBase     res       = new ResponseBase();
            var              serl      = new System.Web.Script.Serialization.JavaScriptSerializer();
            MusicSearchModel music     = serl.Deserialize <MusicSearchModel>(editedMusic);
            var              dbResTags = new List <Tag>();

            if (tagIdsStr != null && tagIdsStr.Length > 0)
            {
                IEnumerable <Guid> tagIds = tagIdsStr.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)
                                            .Select(t => Guid.Parse(t));
                if (tagIds.Any())
                {
                    dbResTags = _tgService.GetTagList(tagIds)
                                .Entities
                                .Where(t => t.IsPublicTag)
                                .ToList();
                }
            }

            var         user = User.GetUser();
            MusicHelper mh   = new MusicHelper();

            if (music.ID == null)
            {
                Music msc = new Music();
                msc.CreateDate = DateTime.Now;
                msc.ArtistID   = user.Id;
                msc.Artist     = user.Artist;
                msc.Status     = MusicActiveStatus.New;
                msc.Tags       = dbResTags;
                var newInSystemTagRes = _tgService.GetNewInSystemTag();
                if (newInSystemTagRes.Success)
                {
                    msc.Tags.Add(newInSystemTagRes.Entity);
                }
                var files = mh.SetMusicBeforeUpDateForArtist(msc, music, WAVFile, MP3File);
                res = _mscService.Add(msc, files.wavFile, files.mp3File);
                if (res.Success)
                {
                    var confirm = new Confirmation();
                    confirm.ConfirmType = ConfirmType.NewMusic;
                    confirm.DateUpdate  = DateTime.Now;
                    confirm.Name        = msc.HebrewName;
                    confirm.EntityId    = msc.ID.ToString();
                    res = _cnfrmService.Add(confirm);
                }
            }
            else
            {
                var getMusicRes = _mscService.GetByID(music.ID.Value);
                if (getMusicRes.Success && getMusicRes.Entity.ArtistID == user.Id)
                {
                    var msc = new Music(getMusicRes.Entity);
                    msc.Tags = msc.Tags
                               .Where(t => !t.IsPublicTag)
                               .Union(dbResTags)
                               .ToList();
                    var files = mh.SetMusicBeforeUpDateForArtist(msc, music, WAVFile, MP3File);

                    if (msc.Status == MusicActiveStatus.Public)
                    {
                        msc.Status        = MusicActiveStatus.WaitingForConfirm;
                        msc.SourceMusicId = getMusicRes.Entity.ID;
                        msc.ID            = 0;
                        res = _mscService.Add(msc, files.wavFile, files.mp3File);
                        if (res.Success)
                        {
                            getMusicRes.Entity.Status = MusicActiveStatus.Edited;
                            var confirm = new Confirmation();
                            confirm.ConfirmType = ConfirmType.UpdateMusic;
                            confirm.DateUpdate  = DateTime.Now;
                            confirm.Name        = msc.HebrewName;
                            confirm.EntityId    = msc.ID.ToString();
                            res = _cnfrmService.Add(confirm);
                        }
                    }
                    else if (msc.Status == MusicActiveStatus.New || msc.Status == MusicActiveStatus.WaitingForConfirm)
                    {
                        var confirmRes = _cnfrmService.GetByEntityId(msc.ID.ToString());
                        if (confirmRes.Success && confirmRes.Entity == null)
                        {
                            var confirm = new Confirmation();
                            confirm.ConfirmType =
                                msc.Status == MusicActiveStatus.New
                                    ? ConfirmType.NewMusic
                                    : ConfirmType.UpdateMusic;
                            confirm.DateUpdate = DateTime.Now;
                            confirm.Name       = msc.HebrewName;
                            confirm.EntityId   = msc.ID.ToString();
                            res = _cnfrmService.Add(confirm);
                        }
                        res = _mscService.Update(msc, msc.ID, files.wavFile, files.mp3File);
                    }
                }
                else
                {
                    res.Success = false;
                }
            }

            return(Json(res));
        }