예제 #1
0
        public async Task <IActionResult> UpdateBand(int bandId, [FromBody] BandDto band)
        {
            try
            {
                if (band != null)
                {
                    if (!ModelState.IsValid)
                    {
                        return(BadRequest(ModelState));
                    }

                    var bandToUpdate = _recordsStoreRepository.GetBandById(bandId);

                    if (bandToUpdate == null)
                    {
                        return(NotFound());
                    }

                    Mapper.Map(band, bandToUpdate);

                    if (await _recordsStoreRepository.SaveChangesAsync())
                    {
                        return(NoContent());
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogCritical($"Exception while updating the band with ID {bandId}", ex);
            }

            return(StatusCode(500, "A problem happened while handling your request."));
        }
예제 #2
0
        public async Task <IActionResult> CreateRecord(int bandId, [FromBody] RecordDto record)
        {
            try
            {
                if (record != null)
                {
                    if (!ModelState.IsValid)
                    {
                        return(BadRequest(ModelState));
                    }

                    if (_recordsStoreRepository.GetBandById(bandId) == null)
                    {
                        return(NotFound());
                    }

                    if (_recordsStoreRepository.GetRecordByTitle(record.Title).Count() != 0)
                    {
                        return(BadRequest($"The record {record.Title} already exists."));
                    }

                    var newRecord = Mapper.Map <Record>(record);

                    _recordsStoreRepository.AddRecord(bandId, newRecord);

                    if (await _recordsStoreRepository.SaveChangesAsync())
                    {
                        var recordToReturn = Mapper.Map <RecordDto>(newRecord);

                        return(Created($"/api/bands/{bandId}/records/{recordToReturn.Id}", recordToReturn));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogCritical("Exception while creating the record", ex);
            }

            return(StatusCode(500, "A problem happened while handling your request."));
        }