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

                    if (!_recordsStoreRepository.GetBandByName(band.Name).Equals(null))
                    {
                        return(BadRequest($"The band {band.Name} already exists."));
                    }

                    var newBand = Mapper.Map <Band>(band);

                    _recordsStoreRepository.AddBand(newBand);

                    if (await _recordsStoreRepository.SaveChangesAsync())
                    {
                        var newBandToReturn = Mapper.Map <BandDto>(newBand);

                        return(CreatedAtRoute("GetBand", new { bandName = newBandToReturn.Name }, newBandToReturn));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogCritical("Exception while creating the band", 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."));
        }