Exemplo n.º 1
0
        public IActionResult ListFiles([FromRoute] string id, [FromQuery] int version)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            else if (!artifactExists(id))
            {
                return(NotFound(id));
            }

            var artifact = _context.GetFullTable <Artifact>().Single(ar => ar.ID == id);

            if (version != 0 && artifact.Version > version)
            {
                var artifactBackup = artifact.Backups.SingleOrDefault(ab => ab.Version == version);
                var data           = new { Artifact = artifact.ID, Files = new List <ArtifactStorageResponse>(), _self = this.FullURL(nameof(ListFiles), new { id, version }) };

                if (artifactBackup == null)
                {
                    return(NotFound("Version {version} is not stored on the server."));
                }

                foreach (var item in artifactBackup.StorageItems)
                {
                    data.Files.Add(new ArtifactStorageResponse
                    {
                        FileName = item.Filename,
                        Url      = this.FullURL(nameof(DownloadArtifact), new { id, version, filename = item.Filename })
                    });
                }

                return(Ok(data));
            }
            else
            {
                var data = new { Artifact = artifact.ID, Files = new List <ArtifactStorageResponse>(), _self = this.FullURL(nameof(ListFiles), new { id }) };

                foreach (var item in artifact.StorageItems)
                {
                    data.Files.Add(new ArtifactStorageResponse
                    {
                        FileName = item.Filename,
                        Url      = this.FullURL(nameof(DownloadArtifact), new { id, filename = item.Filename })
                    });
                }

                return(Ok(data));
            }
        }
Exemplo n.º 2
0
        internal LoginToken GenerateOrGetToken(ApplicationUser user)
        {
            var tokens = _context.GetFullTable <LoginToken>();

            if (tokens.Any((lt) => lt.User.UserName == user.UserName))
            {
                var token = tokens.Where((lt) => lt.User.UserName == user.UserName).Single();

                if (token.Valid > DateTime.Now)
                {
                    return(token);
                }
                else
                {
                    token.Token = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
                    token.Valid = DateTime.Now.AddMinutes(_tokenLifespanInMinutes);
                    _context.SaveChanges();
                    return(token);
                }
            }
            else
            {
                var token = new LoginToken()
                {
                    Token = Convert.ToBase64String(Guid.NewGuid().ToByteArray()), User = user, Valid = DateTime.Now.AddMinutes(_tokenLifespanInMinutes)
                };
                _context.Tokens.Add(token);
                _context.SaveChanges();
                return(token);
            }
        }
Exemplo n.º 3
0
        public async Task <IActionResult> GetLecture([FromRoute, Required] string id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var lectureItem = await _context.GetFullTable <Lecture>().SingleOrDefaultAsync(m => m.ID == id);

            if (lectureItem == null)
            {
                return(NotFound(id));
            }

            var data = new LectureResponse {
                ID = lectureItem.ID, Name = lectureItem.Name, Content = new List <Package>()
            };

            if (lectureItem.Contents != null)
            {
                foreach (var li in lectureItem.Contents)
                {
                    data.Content.Add(li);
                }
            }

            return(Ok(data));
        }
Exemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="_context"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public static LoginToken GetLoginToken(this APIDatabaseContext _context, string token)
        {
            if (string.IsNullOrWhiteSpace(token))
            {
                return(default(LoginToken));
            }

            return(_context.GetFullTable <LoginToken>().SingleOrDefault(t => t.Token == token));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> AddPackageDependency([FromRoute] int id, [FromRoute] int rid)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var packages = _context.GetFullTable <Package>();

            var packageItem = await packages.SingleOrDefaultAsync(m => m.ID == id);

            var rPackageItem = await packages.SingleOrDefaultAsync(m => m.ID == rid);

            if (packageItem == null || rPackageItem == null)
            {
                return(NotFound());
            }

            if (!packageItem.Dependencies.Contains(rPackageItem))
            {
                try
                {
                    packageItem.Dependencies.Add(rPackageItem);
                    _context.Packages.Update(packageItem);
                    await _context.SaveChangesAsync();
                }
                catch (Exception e)
                {
                    Tracer.TraceMessage(e.Message);
                    return(StatusCode(500));
                }
            }

            packageItem.Version++;

            return(Ok(packageItem));
        }
Exemplo n.º 6
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="_context"></param>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public static T GetSingleOrDefault <T>(this APIDatabaseContext _context, Func <T, bool> predicate)
        {
            var queryable = _context.GetFullTable <T>();

            return(queryable != default(IQueryable <T>) ? queryable.SingleOrDefault(predicate) : default(T));
        }