コード例 #1
0
        public async Task <IHttpActionResult> PutPerson(int id, Person person)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != person.Id)
            {
                return(BadRequest());
            }

            db.Entry(person).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PersonExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
コード例 #2
0
        public async Task <IHttpActionResult> PutInstructor(int id, Instructor instructor)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != instructor.InstructorID)
            {
                return(BadRequest());
            }

            db.Entry(instructor).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!InstructorExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
コード例 #3
0
        public async Task DeleteUrl(int id)
        {
            // Delete the Url from the database with the specified ID
            var url = _context.Urls.Where(u => u.Id == id).FirstOrDefault();

            if (url == null)
            {
                throw new Exception("Not found");
            }

            _context.Urls.Remove(url);

            await _context.SaveChangesAsync();
        }
コード例 #4
0
        public async Task <IActionResult> UrlRedirect(string url)
        {
            string redirect = FindUrl(url);

            if (redirect != null)
            {
                await db.SaveChangesAsync();

                return(Redirect(redirect));
            }
            else
            {
                return(Content("No such link!"));
            }
        }
コード例 #5
0
        public async Task <IActionResult> Shorten(String uriList)
        {
            if (string.IsNullOrEmpty(uriList))
            {
                return(BadRequest(new { Error = "Invalid URI" }));
            }

            var uriArray = uriList.Trim().Split(" ");

            foreach (var uriName in uriArray)
            {
                if (!isValidUri(uriName))
                {
                    return(BadRequest(new { Error = String.Format("Invalid URI {0}", uriName) }));
                }
            }

            var flagNewToken = false;
            var listUrls     = new List <Url>();

            foreach (var uriName in uriArray)
            {
                if (!_context.Urls.Any(u => u.LongUrl == uriName))
                {
                    var newToken = RandomString();
                    while (_context.Urls.Any(t => t.Token == newToken))
                    {
                        newToken = RandomString();
                    }

                    var urlItem = new Url()
                    {
                        Token   = newToken,
                        LongUrl = uriName
                    };
                    _context.Urls.Add(urlItem);
                    listUrls.Add(urlItem);
                    await _context.SaveChangesAsync();

                    flagNewToken = true;
                }
                else
                {
                    var urlItem = new Url()
                    {
                        Token   = _context.Urls.Where(u => u.LongUrl == uriName).First().Token,
                        LongUrl = uriName
                    };
                    listUrls.Add(urlItem);
                }
            }
            return(flagNewToken ? StatusCode((int)HttpStatusCode.Created, listUrls) : StatusCode((int)HttpStatusCode.OK, listUrls));
        }
コード例 #6
0
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            using (var context = new UrlContext())
            {
                var url = await context.Urls.Include(s => s.Statistics).SingleOrDefaultAsync(m => m.Id == id);

                context.Statistics.RemoveRange(url.Statistics);
                context.Urls.Remove(url);
                await context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
        }
コード例 #7
0
        public async Task <IActionResult> PUT([FromServices] UrlContext dbContext)
        {
            var body = Request.Form;

            if (body.ContainsKey("urls"))
            {
                var newUrls = new Url
                {
                    longUrl = body["urls"]
                };
                dbContext.Urls.Add(newUrls);
                await dbContext.SaveChangesAsync();

                newUrls.shortUrl       = Service.ShortUrl.getShorted(newUrls.id);
                newUrls.revokePassword = Service.ShortUrl.GenerateRevokePwd();
                await dbContext.SaveChangesAsync();

                Response.StatusCode = 201;
                return(new JsonResult(new
                {
                    code = 201,
                    data = new
                    {
                        shortUrl = $"{Configuration.GetSection("SiteSettings")["prefix"]}/{newUrls.shortUrl}",
                        revokePwd = newUrls.revokePassword
                    }
                }));
            }
            else
            {
                Response.StatusCode = 406;
                return(new JsonResult(new
                {
                    code = 406,
                    msg = "Not Acceptable"
                }));
            }
        }
コード例 #8
0
        public async Task <IActionResult> Create(Url url)
        {
            using (var context = new UrlContext())
            {
                string referer = Request.Headers["Referer"].ToString() ?? string.Empty;

                var item = await context.Urls.Where(o => o.Id == url.Id).FirstOrDefaultAsync();

                if (item != null)
                {
                    Url shortUrl = await urlService.MakeShort(item.LongUrl, referer);

                    item.ShortUrl = shortUrl.ShortUrl;
                    item.ShortUrl = string.Format("{0}://{1}{2}.co", Request.Scheme, Url.Content("~"), item.ShortUrl);
                    context.Urls.Update(item);
                    await context.SaveChangesAsync();
                }
                else
                {
                    try
                    {
                        Url shortUrl = await urlService.MakeShort(url.LongUrl, referer);

                        shortUrl.ShortUrl = string.Format("{0}://{1}{2}.co", Request.Scheme, Url.Content("~"), shortUrl.ShortUrl);
                        context.Urls.Add(shortUrl);
                        await context.SaveChangesAsync();
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError("", ex.Message);
                    }
                }
            }

            return(RedirectToAction("Index"));
        }
コード例 #9
0
        public async Task <IGkamaUrl> AddUrlAsync(string target)
        {
            var url = new GkamaUrl()
            {
                Target = target
            };

            await _context.Urls
            .AddAsync(url);

            await _context.SaveChangesAsync();

            return(url);
        }
コード例 #10
0
        public async Task <IActionResult> Delete(string shorten, [FromQuery] string revokePwd, [FromServices] UrlContext dbContext)
        {
            var query = from urls in dbContext.Urls
                        where urls.shortUrl == shorten
                        select urls;

            if (query.Count() == 0)
            {
                Response.StatusCode = 201;
                return(new JsonResult(new
                {
                    code = 201,
                    msg = "Deleted"
                }));
            }
            else
            {
                var target = query.Single();
                if (target.revokePassword == revokePwd)
                {
                    dbContext.Urls.Remove(target);
                    await dbContext.SaveChangesAsync();

                    Response.StatusCode = 201;
                    return(new JsonResult(new
                    {
                        code = 201,
                        msg = "Deleted"
                    }));
                }
                else
                {
                    Response.StatusCode = 401;
                    return(new JsonResult(new
                    {
                        code = 401,
                        msg = "Invaild Revoke password"
                    }));
                }
            }
        }