Esempio n. 1
0
        public IActionResult Post([FromBody] FormDTO formDto)
        {
            try
            {
                Uri uri = new Uri(formDto.Uri);
                formDto.Uri = uri.AbsoluteUri;
            }
            catch (UriFormatException ex)
            {
                var res = new Dictionary <string, string>();
                res.Add("Uri", "Invalid Uri.");
                return(BadRequest(res));
            }


            var uriModel = _uriRepository.FindByField(FIELD_ORIGINAL, formDto.Uri);

            if (uriModel != null)
            {
                formDto.Alias = GetBaseUrl(Request, uriModel.Alias);
                return(Ok(formDto));
            }
            else
            {
                string token = null;
                uriModel = null;
                for (var count = 0; count < 20; count++)
                {
                    token    = Token.Generate();
                    uriModel = _uriRepository.FindByField(FIELD_ALIAS, token);
                    if (uriModel == null)
                    {
                        break;
                    }
                }

                uriModel = new UriModel
                {
                    Original = formDto.Uri,
                    Alias    = token
                };

                _uriRepository.Add(uriModel);

                formDto.Alias = GetBaseUrl(Request, token);

                return(Created("", formDto));
            }
        }
Esempio n. 2
0
        public ActionResult Index()
        {
            ViewBag.Title = "UrlShortener";

            // TODO: instantiate a repository
            // add something to it for testing
            // store the repository in a Session variable
            // UGH SESSION VARS

            var repo = new UriRepository();
            var uri  = new UriModel(new Helpers.Utility(), "http://www.michaelpaulukonis.com");

            // TODO: controller's dependencies should be mocked!
            repo.Add(uri);
            Session["repo"] = repo;

            var location = new LocationModel();

            return(View(location));
        }