Esempio n. 1
0
        public async Task <IActionResult> Edit(Guid guid, [FromBody] ShortLinkModel newShortLink)
        {
            var shortLink = Db.ShortLinks.Find(guid);

            if (shortLink == null)
            {
                return(NotFound(ErrorModel.NotFound()));
            }

            if (!shortLink.ValidateIdent())
            {
                return(BadRequest(ErrorModel.BadRequest("invalid short ident")));
            }

            if (!CheckShortIdent(newShortLink, shortLink.GUID))
            {
                return(BadRequest(ErrorModel.BadRequest("short ident already in use")));
            }

            if (!await newShortLink.ValidateURI())
            {
                return(BadRequest(ErrorModel.BadRequest("invalid root link")));
            }

            shortLink.Update(newShortLink);
            shortLink.Sanitize();

            Db.ShortLinks.Update(shortLink);
            await Db.SaveChangesAsync();

            return(Ok(shortLink));
        }
Esempio n. 2
0
        public async Task <IActionResult> Create([FromBody] ShortLinkModel shortLink)
        {
            if (!shortLink.ValidateIdent())
            {
                return(BadRequest(ErrorModel.BadRequest("invalid short ident")));
            }

            if (!CheckShortIdent(shortLink))
            {
                return(BadRequest(ErrorModel.BadRequest("short ident already in use")));
            }

            if (!await shortLink.ValidateURI())
            {
                return(BadRequest(ErrorModel.BadRequest("invalid root link")));
            }

            shortLink.Sanitize(asNew: true);

            Db.ShortLinks.Add(shortLink);
            await Db.SaveChangesAsync();

            return(Created(shortLink.GUID.ToString(), shortLink));
        }