Beispiel #1
0
        public static async Task <IActionResult> Create(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "artist")] Artist data,
            ILogger log,
            CancellationToken cancellationToken = default)
        {
            log.LogInformation("Start of {method}", nameof(Create));

            var collection = MongoCollectionFactory.Get <Artist>(Constants.ARTIST);
            await collection.InsertOneAsync(data);

            log.LogInformation("End of {method}", nameof(Create));
            return(new OkObjectResult(data));
        }
Beispiel #2
0
        public static async Task <IActionResult> List(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "album")] HttpRequest request,
            ILogger log,
            CancellationToken cancellationToken = default)
        {
            log.LogInformation("Start of {method}", nameof(List));

            var collection = MongoCollectionFactory.Get <Album>(Constants.ALBUM);

            var result = await collection.FindAsync(FilterDefinition <Album> .Empty, cancellationToken : cancellationToken);

            var list = await result.ToListAsync(cancellationToken);

            log.LogInformation("End of {method}", nameof(List));
            return(new OkObjectResult(list));
        }
Beispiel #3
0
        public static async Task <IActionResult> Update(
            [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "artist/{artistId}")] Artist data,
            string artistId,
            ILogger log,
            CancellationToken cancellationToken = default)
        {
            log.LogInformation("Start of {method}", nameof(Update));

            data.Id = ObjectId.Parse(artistId);

            var collection = MongoCollectionFactory.Get <Artist>(Constants.ARTIST);

            await collection.ReplaceOneAsync(u => u.Id.Equals(artistId), data, cancellationToken : cancellationToken);

            log.LogInformation("End of {method}", nameof(Update));
            return(new OkObjectResult(data));
        }
Beispiel #4
0
        static async Task <Artist> GetArtistByName(string artistName, CancellationToken cancellationToken = default)
        {
            if (string.IsNullOrEmpty(artistName))
            {
                throw new ArgumentNullException(nameof(artistName));
            }

            var collection = MongoCollectionFactory.Get <Artist>(Constants.ARTIST);

            FindOptions <Artist, Artist> opts = new FindOptions <Artist, Artist>()
            {
                AllowPartialResults = false,
                BatchSize           = 1,
            };

            var album = await collection.FindAsync(x => x.Name == artistName, opts, cancellationToken : cancellationToken);

            return(await album.FirstOrDefaultAsync(cancellationToken));
        }
Beispiel #5
0
        public static async Task <IActionResult> Create(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "album")] Album data,
            ILogger log,
            CancellationToken cancellationToken = default)
        {
            log.LogInformation("Start of {method}", nameof(Create));

            var artist = string.IsNullOrEmpty(data.Artist?.Name) && string.IsNullOrEmpty(data.ArtistName)
                ? default
                : await GetArtistByName(data.Artist?.Name ?? data.ArtistName);

            var genre = string.IsNullOrEmpty(data.Genre?.Name) && string.IsNullOrEmpty(data.GenreName)
                ? default
                : await GetGenreByName(data.Genre?.Name ?? data.GenreName);

            data.Artist = artist;
            data.Genre  = genre;

            var collection = MongoCollectionFactory.Get <Album>(Constants.ALBUM);
            await collection.InsertOneAsync(data);

            log.LogInformation("End of {method}", nameof(Create));
            return(new OkObjectResult(data));
        }