Beispiel #1
0
        public bool Create(TorrentDto torrentDto)
        {
            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                var torrent = new Torrent()
                {
                    Id              = torrentDto.Id,
                    IsDeleted       = false,
                    DeletedOn       = torrentDto.DeletedOn,
                    Title           = torrentDto.Title,
                    Description     = torrentDto.Description,
                    TimesDownloaded = 0,
                    UploadedOn      = DateTime.Now,
                    UploaderId      = torrentDto.Uploader.Id,
                    CatalogId       = torrentDto.Catalog.Id,
                    SubTypeId       = torrentDto.SybType.Id
                };

                CatalogDto catalog = catalogService.GetById(torrent.CatalogId);
                catalog.TorrentNum += 1;

                if (!catalogService.Update(catalog))
                {
                    return(false);
                }

                unitOfWork.TorrentRepository.Create(torrent);

                return(unitOfWork.Save());
            }
        }
        public IActionResult Create([FromBody] TorrentDto torrent)
        {
            if (torrentService.Create(torrent))
            {
                return(NoContent());
            }

            return(BadRequest());
        }
Beispiel #3
0
        /// <summary>
        /// Gets the torrent.
        /// </summary>
        /// <param name="id">The torrent identifier.</param>
        public TorrentDto GetTorrent(int id)
        {
            TorrentDto torrent = repository.GetTorrent(id);

            if (torrent != null)
            {
                return(torrent);
            }

            throw new ArgumentException("This topic doesn't exist");
        }
        public IActionResult Update([FromRoute] int id, [FromBody] TorrentDto torrent)
        {
            torrent.Id = id;

            if (torrentService.Update(torrent))
            {
                return(NoContent());
            }

            return(BadRequest());
        }
Beispiel #5
0
        /// <summary>
        /// Adds the torrent.
        /// </summary>
        /// <param name="torrentDto">The torrent dto.</param>
        /// <param name="image">The image of torrent.</param>
        /// <param name="file">The torrentfile.</param>
        public void AddTorrent(TorrentDto torrentDto, HttpPostedFileBase image, HttpPostedFileBase file)
        {
            if (image != null && file != null)
            {
                string imageName = System.IO.Path.GetFileName(image.FileName);
                string imagePath = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~/Content/Images"), imageName);
                image.SaveAs(imagePath);

                string fileName = System.IO.Path.GetFileName(file.FileName);
                string filePath = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~/TorrentFiles"), fileName);
                file.SaveAs(filePath);

                torrentDto.Characteristics.Add("12", "/Content/Images/" + imageName);

                TorrentFile torrentFile = new TorrentFile
                {
                    Path           = "/TorrentFiles/" + fileName,
                    Size           = torrentDto.TorrentFile.Size,
                    DownloadsCount = 0,
                    AddedDate      = DateTime.Now
                };
                this.context.TorrentFiles.Add(torrentFile);
                this.context.SaveChanges();

                Torrent torrent = new Torrent
                {
                    TorrentTypeID = this.context.TorrentTypes
                                    .Where(tp => tp.Name == torrentDto.TorrentType)
                                    .Select(tp => tp.TorrentTypeID)
                                    .SingleOrDefault(),
                    TorrentFileID = this.context.TorrentFiles
                                    .Where(tf => tf.Path == torrentFile.Path)
                                    .Select(tf => tf.TorrentFileID)
                                    .SingleOrDefault()
                };

                this.context.Torrents.Add(torrent);
                this.context.SaveChanges();

                foreach (string key in torrentDto.Characteristics.Keys)
                {
                    this.context.TorrentCharacteristics.Add(new TorrentCharacteristic
                    {
                        TorrentID           = torrent.TorrentID,
                        CharacteristicID    = Convert.ToInt32(key),
                        CharacteristicValue = torrentDto.Characteristics[key]
                    });
                }
                this.context.SaveChanges();
            }
        }
Beispiel #6
0
        public bool Update(TorrentDto torrentDto)
        {
            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                var result = unitOfWork.TorrentRepository.GetById(torrentDto.Id);

                if (result == null)
                {
                    return(false);
                }

                int oldCatalog = result.CatalogId;
                int newCatalog = torrentDto.Catalog.Id;

                result.Id          = torrentDto.Id;
                result.IsDeleted   = torrentDto.IsDeleted;
                result.DeletedOn   = torrentDto.DeletedOn;
                result.Title       = torrentDto.Title;
                result.Description = torrentDto.Description;
                result.CatalogId   = torrentDto.Catalog.Id;
                result.SubTypeId   = torrentDto.SybType.Id;

                CatalogDto catalogNew = catalogService.GetById(newCatalog);
                CatalogDto catalogOld = catalogService.GetById(oldCatalog);
                catalogNew.TorrentNum += 1;
                catalogOld.TorrentNum -= 1;

                if (!catalogService.Update(catalogNew))
                {
                    return(false);
                }

                if (!catalogService.Update(catalogOld))
                {
                    return(false);
                }

                unitOfWork.TorrentRepository.Update(result);

                return(unitOfWork.Save());
            }
        }
Beispiel #7
0
 /// <summary>
 /// Adds the torrent.
 /// </summary>
 /// <param name="torrentDto">The torrent dto.</param>
 /// <param name="image">The image of torrent.</param>
 /// <param name="file">The torrent file.</param>
 public void AddTorrent(TorrentDto torrentDto, HttpPostedFileBase image, HttpPostedFileBase file)
 {
     repository.AddTorrent(torrentDto, image, file);
 }