Example #1
0
        public async Task <IActionResult> GetAlbumDetails(string albumId)
        {
            if (!int.TryParse(albumId, out var validAlbumId))
            {
                return(UnprocessableEntity());
            }

            var albumDetailsOption = await _albumsRepository.GetAlbumDetails(validAlbumId);

            if (!albumDetailsOption.TryUnwrap(out var albumDetails))
            {
                _logger.Warning($"Cannot find album details with album id: {albumId}.");
                return(NotFound());
            }

            var links = HypermediaLinkBuilder.ForAlbumDetailsDiscovery(Url, validAlbumId, albumDetails.UserId);

            return(Ok(
                       new AlbumResponse(
                           links,
                           new Dictionary <string, string> {
                { "id", albumDetails.Id.ToString() },
                { "userId", albumDetails.UserId.ToString() },
                { "title", albumDetails.Title }
            }
                           )
                       ));
        }
Example #2
0
        public async Task <IActionResult> GetUserAlbums(string userId)
        {
            if (!int.TryParse(userId, out var validUserId))
            {
                return(UnprocessableEntity());
            }

            var albumsForUser = await _albumsRepository.GetAlbumByUserId(validUserId);

            if (!albumsForUser.Any())
            {
                return(NotFound());
            }

            return(Ok(
                       new UserResponse(
                           HypermediaLinkBuilder.ForUsersDiscovery(Url, validUserId),
                           new Dictionary <string, string>()
            {
                { "userId", userId }
            },
                           albumsForUser
                           )
                       ));
        }
Example #3
0
        public async Task <IActionResult> Post([FromBody] CustomerRequestModel customerRequestModel)
        {
            var customer = customerRequestModel.ToDomainType();
            await _customersRepository.CreateCustomer(customer);

            _logger.Information($"Created customer with customer reference: {customer.CustomerReference}.");

            var links = HypermediaLinkBuilder.ForCustomerDiscovery(Url, customer.CustomerReference.ToString());
            var customerResponseData = new Dictionary <string, string>
            {
                { "customer_reference", customer.CustomerReference.ToString() }
            };
            var response = new CreatedResponse <CustomerDiscovery>("customer_created", links, customerResponseData);

            return(Created(string.Empty, response));
        }
Example #4
0
        public async Task <IActionResult> Get()
        {
            var albums = await _albumsRepository.GetAlbums();

            if (!albums.Any())
            {
                return(NotFound());
            }

            return(Ok(
                       albums.Select(
                           x => new AlbumResponse(
                               HypermediaLinkBuilder.ForAlbumDiscovery(Url, x.Id, x.UserId),
                               new Dictionary <string, string> {
                { "id", x.Id.ToString() },
                { "userId", x.UserId.ToString() },
                { "title", x.Title }
            }))));
        }
Example #5
0
        public async Task <IActionResult> Put(string customerReference, [FromBody] CustomerRequestModel customerRequestModel)
        {
            if (!Guid.TryParse(customerReference, out var guid))
            {
                _logger.Warning($"Could not parse customer reference: {customerReference}.");
                var errorResponse = new ValidationError("request_invalid", new List <string> {
                    ErrorCodes.CustomerReferenceInvalid
                });
                return(new Models.BadRequestObjectResult(errorResponse));
            }

            var customerOption = await _customersRepository.GetCustomerByCustomerReference(guid);

            if (!customerOption.TryUnwrap(out var customer))
            {
                _logger.Warning($"Cannot find customer with customer reference: {customerReference}.");
                return(NotFound());
            }

            var customertoUpdate = customerRequestModel.ToDomainType();
            var updatedCustomer  = new UpdateCustomer(
                customer.CustomerReference,
                customertoUpdate.FirstName,
                customertoUpdate.Surname,
                customertoUpdate.Status,
                customer.CreatedDate,
                customer.LastModifiedDate);

            var customerToSave = _customerService.UpdateCustomer(updatedCustomer);
            await _customersRepository.UpdateCustomer(customerToSave);

            _logger.Information($"Updated customer with customer reference: {customer.CustomerReference}.");

            var links    = HypermediaLinkBuilder.ForCustomerDiscovery(Url, customer.CustomerReference.ToString());
            var response = new CreatedResponse <CustomerDiscovery>("customer_updated", links);

            return(Ok(response));
        }
Example #6
0
        public async Task <IActionResult> GetPhotos(string albumId)
        {
            if (!int.TryParse(albumId, out var validAlbumId))
            {
                return(UnprocessableEntity());
            }

            var photos = await _photosRepository.GetPhotosByAlbumId(validAlbumId);

            if (!photos.Any())
            {
                return(NotFound());
            }

            return(Ok(
                       new PhotosResponse(
                           HypermediaLinkBuilder.ForPhotosDiscovery(Url, validAlbumId),
                           new Dictionary <string, string> {
                { "albumId", validAlbumId.ToString() }
            },
                           photos
                           )
                       ));
        }
        public IActionResult Get()
        {
            var model = new HypermediaLinks <ApiDiscovery>(HypermediaLinkBuilder.ForServiceDiscovery(Url));

            return(Ok(model));
        }