예제 #1
0
        public ActionResult <RegisterUrlResponseBody> RegisterUrl([FromBody] RegisterUrlRequestBody request)
        {
            var accountClaim = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier);

            if (accountClaim == null)
            {
                return(Unauthorized());
            }
            var account = accountClaim.Value;

            if (account == null)
            {
                return(Unauthorized());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            RegisteredUrl           registeredUrl = _urlService.CreateRegisteredUrl(request, account);
            RegisterUrlResponseBody response      = new RegisterUrlResponseBody()
            {
                shortUrl = $"http://localhost:6500/{registeredUrl.ShortUrl}"
            };

            return(Created("", response));
        }
예제 #2
0
        /**
         * metoda vraca registrirani URL
         *
         * Ako za korisnika 'Account' vec postoji URL kao u 'Request'u, onda vraćamo taj postojeći objekt iz baze
         * Ako URL ne postoji, kreiramo novi RegisteredUrl, spremamo u bazu i vraćamo ga
         */
        public RegisteredUrl CreateRegisteredUrl(RegisterUrlRequestBody Request, string Account)
        {
            //pokusavamo naci url u bazi podataka. ako postoji, provjeravamo da li pripada ovom accountu.
            //ako je sve zo zadovoljeno, nema potrebe kreirati novi zapis, nego vracamo stari
            RegisteredUrl findUrl = _context.RegisteredUrls.FirstOrDefault(databaseUrl => databaseUrl.LongUrl == Request.url && databaseUrl.AccountID == Account);

            if (findUrl != null)
            {
                return(findUrl);
            }
            else
            {
                //URL ne postoji, kreiramo novi zapis u bazi i vracamo novi RegisteredUrl objekt
                RegisteredUrl registeredUrl = new RegisteredUrl()
                {
                    LongUrl = Request.url, ShortUrl = ShortUrlGenerator.Generate(10), AccountID = Account
                };
                if (Request.redirectType > 0)
                {
                    registeredUrl.RedirectType = Request.redirectType;
                }
                else
                {
                    registeredUrl.RedirectType = 302;
                }
                _context.RegisteredUrls.Add(registeredUrl);
                _context.SaveChanges();
                return(registeredUrl);
            }
        }
예제 #3
0
        /**
         *  metoda prima ID registriranog URL objekta i uvecava njegov numberOfCalls za 1, za potrebe statistike
         */
        public void IncrementStatistic(long ID)
        {
            RegisteredUrl findUrl = _context.RegisteredUrls.Find(ID);

            if (findUrl != null)
            {
                findUrl.numberOfCalls        += 1;
                _context.Entry(findUrl).State = EntityState.Modified;
                _context.SaveChanges();
            }
        }
예제 #4
0
        /**
         *  metoda vraca registrirani url objekt iz baze na temelju ShortUrl stringa
         */
        public RegisteredUrl GetRegisteredUrl(string ShortUrl)
        {
            RegisteredUrl findUrl = _context.RegisteredUrls.FirstOrDefault(databaseUrl => databaseUrl.ShortUrl == ShortUrl);

            return(findUrl);
        }