Exemple #1
0
        /// <summary>
        /// Persists a linked resource
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public LinkedResourceDto Save(LinkedResourceDto dto)
        {
            try
            {
                var entity = new LinkedResource();

                if (dto.Id > 0)
                {
                    entity = _dbContext.LinkedResources.SingleOrDefault(x => x.UID == dto.Id);
                }
                else
                {
                    _dbContext.LinkedResources.Add(entity);
                }

                // Commit
                Mappings.Map(ref entity, dto);
                _dbContext.SaveChanges();

                return(Mappings.Map(entity));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
            }

            return(null);
        }
Exemple #2
0
        public static void Map(ref LinkedResource entity, LinkedResourceDto dto)
        {
            if (dto.Id > 0)
            {
                entity.UID = dto.Id;
            }

            entity.Url     = dto.Url;
            entity.Created = dto.Created;
        }
Exemple #3
0
        public IActionResult RetrieveLink([FromRoute] string key)
        {
            var linkedResource = new LinkedResourceDto {
                Key = key
            };

            // Generate the Id from the supplied Key
            linkedResource.DecodeKey();

            linkedResource = _dataService.LinkedResource.Get(linkedResource.Id);

            if (linkedResource == null)
            {
                return(BadRequest(new ErrorResponse(ErrorCode.INVALID_LINK)));
            }

            return(Ok(linkedResource));
        }
Exemple #4
0
        public void LinkedResourceIsUpdated()
        {
            // Arrange
            var expectedId        = 1;
            var expectedUrl       = "Some.Url";
            var linkedResourceDto = new LinkedResourceDto {
                Id = expectedId, Url = expectedUrl
            };
            var originalUrl = DbSets.LinkedResources.Single(x => x.UID == expectedId).Url;

            var linkedResourceData = new LinkedResourceDataSetup(true);

            // Act
            var actual = linkedResourceData.Scope.Save(linkedResourceDto);

            // Assert
            Assert.IsNotNull(actual);
            Assert.AreEqual(expectedId, actual.Id);
            Assert.AreEqual(expectedUrl, actual.Url);
            Assert.AreNotEqual(originalUrl, actual.Url);
        }
Exemple #5
0
        public void NewLinkIsCreatedWhenLinkDoesNotExist()
        {
            // Arrange
            var expected = new LinkedResourceDto {
                Id = 1, Url = "http://some.site"
            };
            var expectedKey        = "MQ";
            var expectedEncodedUrl = $"http://lync.ninja/{expectedKey}";

            var request = new CreateLinkRequest {
                Url = expected.Url
            };
            var encodedUrlBase = "http://lync.ninja/";

            var linkController = new LinkControllerSetup();

            linkController.Mock_AppSettings.Value.Returns(new AppSettings {
                EncodedUrlBase = encodedUrlBase
            });
            linkController.Mock_DataService.LinkedResource.Get(Arg.Any <string>()).Returns(x => null);
            linkController.Mock_DataService.LinkedResource.Save(Arg.Any <LinkedResourceDto>()).Returns(expected);

            // Act
            var actualResult = linkController.Scope.CreateLink(request);

            // Assert
            var actual = AssertOkResult(actualResult);

            Assert.AreEqual(expected.Url, actual.Url);
            Assert.AreEqual(expected.Id, actual.Id);
            Assert.AreEqual(expectedKey, actual.Key);
            Assert.AreEqual(expectedEncodedUrl, actual.EncodedUrl);

            var decodedKey = Base64.Decode(expected.Key);

            Assert.AreEqual(expected.Id.ToString(), decodedKey);

            linkController.Mock_DataService.LinkedResource.Received(1).Get(Arg.Any <string>());
            linkController.Mock_DataService.LinkedResource.Received(1).Save(Arg.Any <LinkedResourceDto>());
        }
Exemple #6
0
        public void LinkIsDecoded()
        {
            // Arrange
            var expected = new LinkedResourceDto {
                Id = 1, Url = "http://some.site"
            };
            var request = "MQ";

            var linkController = new LinkControllerSetup();

            linkController.Mock_DataService.LinkedResource.Get(Arg.Any <long>()).Returns(expected);

            // Act
            var actualResult = linkController.Scope.RetrieveLink(request);

            // Assert
            var actual = AssertOkResult(actualResult);

            Assert.AreEqual(expected.Url, actual.Url);

            linkController.Mock_DataService.LinkedResource.Received(1).Get(Arg.Is <long>(x => x == expected.Id));
        }