示例#1
0
        public async Task <IActionResult> AddSong(string bandid, string songid)
        {
            // check that song exists
            string otherCollection = CollectionNames.SONGS;
            Song   song;

            try
            {
                song = await CosmosRepo.GetDocument <Song>(otherCollection, songid);
            }
            catch (DocumentClientException)
            {
                return(ItemNotFoundResult(songid, otherCollection));
            }

            return(await ChangeInDB(bandid, band =>
            {
                List <string> songs = band.Songs.ToList();

                if (!songs.Contains(songid))
                {
                    songs.Add(songid);
                    band.Songs = songs;
                }

                return band;
            }));
        }
        public async Task <IActionResult> UploadFile(string partId)
        {
            // get part
            Part part;

            try
            {
                part = await CosmosRepo.GetDocument <Part>(ConstantNames.PARTS, partId);
            }
            catch (DocumentClientException)
            {
                return(ItemNotFoundResult(partId));
            }

            // read contents
            string contents;

            using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
            {
                contents = await reader.ReadToEndAsync();
            }

            // upload blob
            string container = ConstantNames.XMLCONTAINER;
            string blobId    = await BlobRepo.UploadBlob(container, contents);

            string uri = BlobRepo.GetBlobUri(container, blobId);

            // change part
            part.Path = blobId;
            await CosmosRepo.ReplaceDocument <Part>(ConstantNames.PARTS, partId, part);

            return(Created(uri, contents));
        }
        public override async Task <IActionResult> Post([FromBody] Event docIn)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // check required properties
            if (docIn.Name == null)
            {
                return(MissingPropertyResult("name"));
            }
            if (docIn.Band == null)
            {
                return(MissingPropertyResult("band"));
            }

            // remove unused properties
            docIn.Songs       = new List <string>();
            docIn.Users       = new List <string>();
            docIn.CurrentSong = null;

            // get band if it exists
            string bandId         = docIn.Band;
            string bandCollection = CollectionNames.BANDS;
            Band   band           = null;

            try
            {
                band = await CosmosRepo.GetDocument <Band>(bandCollection, bandId);
            }
            catch (DocumentClientException)
            {
                return(ItemNotFoundResult(bandId, bandCollection));
            }

            // create event
            Event created = await CosmosRepo.CreateDocument(CollectionName, docIn);

            string eventId = created.Id;

            // update band
            if (!band.Events.Contains(eventId))
            {
                List <string> events = band.Events.ToList();
                events.Add(eventId);
                band.Events = events;
                await CosmosRepo.ReplaceDocument(bandCollection, bandId, band);
            }

            return(Created(GetGetUri(eventId), created));
        }
        public async Task <IActionResult> NotifySong(string eventid, [FromBody] Event replacement)
        {
            if (replacement.CurrentSong == null)
            {
                return(MissingPropertyResult("currentsong"));
            }

            // replace song
            string        songId = replacement.CurrentSong;
            IActionResult result = await CheckAndChangeInDB <Band>(eventid,
                                                                   event_ => event_.Band,
                                                                   ConstantNames.BANDS,
                                                                   band => band.Songs.Contains(songId),
                                                                   event_ =>
            {
                event_.CurrentSong = songId;
                return(event_);
            },
                                                                   NotFound(SongNotInBandMessage(songId))
                                                                   );

            // get song name
            string songName   = songId;
            Song   songResult = await CosmosRepo.GetDocument <Song>(ConstantNames.SONGS, songId);

            if (songResult != null)
            {
                songName = songResult.Name;
            }

            if (result is OkObjectResult okobj && okobj != null && okobj.Value is Event ev)
            {
                // for each user, send a notification to them
                List <string> userIds = ev.Users.ToList();
                foreach (string userId in userIds)
                {
                    Player u = await CosmosRepo.GetDocument <Player>(ConstantNames.PLAYERS, userId);

                    if (u != null)
                    {
                        string deviceId = u.DeviceId;
                        if (deviceId != null && deviceId != "FAILED_ID")
                        {
                            await notifRepo.SendNotification("Next song", songName, deviceId);
                        }
                    }
                }
            }

            return(result);
        }
        public async Task <IActionResult> RemoveEvent(string bandid, string eventid)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            // GET band
            Band band = await CosmosRepo.GetDocument <Band>(ConstantNames.BANDS, bandid);

            if (band == null)
            {
                return(ItemNotFoundResult(bandid));
            }

            // GET event
            Event ev = await CosmosRepo.GetDocument <Event>(ConstantNames.EVENTS, eventid);

            if (ev == null)
            {
                return(ItemNotFoundResult(eventid));
            }

            // remove event from band
            List <string> events = band.Events.ToList();

            events.Remove(eventid);
            band.Events = events;
            Band result = await CosmosRepo.ReplaceDocument(ConstantNames.BANDS, bandid, band);

            // remove event ID from each user
            foreach (string userid in ev.Users)
            {
                Player player = await CosmosRepo.GetDocument <Player>(ConstantNames.PLAYERS, userid);

                if (player != null)
                {
                    List <string> userEvents = player.Events.ToList();
                    userEvents.Remove(eventid);
                    player.Events = userEvents;
                    await CosmosRepo.ReplaceDocument(ConstantNames.PLAYERS, userid, player);
                }
            }

            return(Ok(result));
        }
        public override async Task <IActionResult> Post([FromBody] Part docIn)
        {
            // make sure required fields are included
            if (docIn.Instrument == null)
            {
                return(MissingPropertyResult("instrument"));
            }
            if (docIn.Song == null)
            {
                return(MissingPropertyResult("song"));
            }
            docIn.Path = null;

            // make sure song exists
            string songsCollection = ConstantNames.SONGS;
            string songId          = docIn.Song;
            Song   song;

            try
            {
                song = await CosmosRepo.GetDocument <Song>(songsCollection, songId);
            }
            catch (DocumentClientException)
            {
                return(ItemNotFoundResult(songId, songsCollection));
            }

            // create part
            Part newDoc = await CosmosRepo.CreateDocument(CollectionName, docIn);

            string partId = newDoc.Id;

            // update song
            if (!song.Parts.Contains(partId))
            {
                List <string> parts = song.Parts.ToList();
                parts.Add(partId);
                song.Parts = parts;
                await CosmosRepo.ReplaceDocument(songsCollection, songId, song);
            }

            return(Created(GetGetUri(newDoc.Id), newDoc));
        }
        public async Task <IActionResult> DownloadFile(string partId)
        {
            // get part
            Part part;

            try
            {
                part = await CosmosRepo.GetDocument <Part>(ConstantNames.PARTS, partId);
            }
            catch (DocumentClientException)
            {
                return(ItemNotFoundResult(partId));
            }

            if (part.Path == null)
            {
                return(BadRequest("Part does not yet have a corresponding music location"));
            }

            string container = ConstantNames.XMLCONTAINER;
            string contents  = await BlobRepo.DownloadBlob(container, part.Path);

            return(Ok(contents));
        }