//Create a new tape
        public int CreateTape(TapeInputModel tape)
        {
            var tapes   = _tapeRepository.GetAllTapes();
            var newTape = Mapper.Map <Tape>(tape);

            return(_tapeRepository.CreateTape(newTape));
        }
Beispiel #2
0
 public IActionResult InitializeTapes()
 {
     // We do not initialize unless database is empty for safety reasons
     if (_tapeService.GetAllTapes().Count > 0)
     {
         return(BadRequest(new ExceptionModel {
             StatusCode = (int)HttpStatusCode.BadRequest,
             Message = "Tapes have already initialized in some form"
         }));
     }
     // Otherwise add tapes from initialization file
     using (StreamReader r = StreamReaderFactory.GetStreamReader("./Resources/tapes.json")) {
         string  json      = r.ReadToEnd();
         dynamic tapesJSON = JsonConvert.DeserializeObject(json);
         foreach (var tapeJSON in tapesJSON)
         {
             // Generate input model from json tape
             TapeInputModel tape = SeedingUtils.ConvertJSONToTapeInputModel(tapeJSON);
             // Check if tape input model is valid
             if (!ModelState.IsValid)
             {
                 IEnumerable <string> errorList = ModelState.Values.SelectMany(v => v.Errors).Select(x => x.ErrorMessage);
                 throw new InputFormatException("Video tape in initialization file improperly formatted.", errorList);
             }
             // Create new tape if input model was valid
             Console.WriteLine($"adding tape {tapeJSON.id} of {tapesJSON.Count}");
             _tapeService.CreateTape(tape);
         }
     }
     return(NoContent());
 }
Beispiel #3
0
        public void AddNewTape_TestIfTapeIsAdded()
        {
            // arrange
            var tapeInput = new TapeInputModel
            {
                Title        = "Bad Taste",
                DirectorName = "Peter Jackson",
                Eidr         = "2",
                ReleaseDate  = new DateTime(1999, 10, 10),
                Type         = "VHS"
            };

            _tapeRepositoryMock.Setup(method => method.AddNewTape(tapeInput)).Returns(
                FizzWare.NBuilder.Builder <TapeDetailsDto>
                .CreateNew().With(x => x.Id = 1).With(x => x.Eidr = "1").With(x => x.Title = "Pulp Fiction")
                .With(x => x.Type           = "VHS").With(x => x.AverageRating = 2.2)
                .With(x => x.DirectorName   = "Quentin Tarantino")
                .With(x => x.ReleaseDate    = DateTime.Today).Build());

            // act
            var newtape = _tapeService.AddNewTape(tapeInput);

            // assert
            Assert.AreEqual("VHS", newtape.Type);
            Assert.IsNotNull(newtape);
        }
Beispiel #4
0
        public void UpdateTape()
        {
            // arrange
            int tapeId    = 1;
            var tapeInput = new TapeInputModel
            {
                Title        = "Pulp Fiction",
                DirectorName = "Peter Jackson",
                Eidr         = "2",
                ReleaseDate  = new DateTime(1999, 10, 10),
                Type         = "VHS"
            };

            _tapeRepositoryMock.Setup(method => method.UpdateTape(tapeInput, tapeId));
            _tapeRepositoryMock.Setup(method => method.GetTapeById(tapeId)).Returns(
                FizzWare.NBuilder.Builder <TapeDetailsDto>
                .CreateNew().With(x => x.Id = 1).With(x => x.Eidr = "2").With(x => x.Title = "Pulp Fiction")
                .With(x => x.Type           = "VHS").With(x => x.AverageRating = 2.2)
                .With(x => x.DirectorName   = "Quentin Tarantino")
                .With(x => x.ReleaseDate    = DateTime.Today).Build());
            // act
            _tapeService.UpdateTape(tapeInput, tapeId);

            // assert
            //_tapeRepositoryMock.Verify(x => x.GetTapeById(tapeId), Times.Once);
            _tapeRepositoryMock.Verify(x => x.UpdateTape(tapeInput, tapeId), Times.Once);
        }
Beispiel #5
0
        public IActionResult UpdateTape([FromBody] TapeInputModel tape, int?id)
        {
            if (!ModelState.IsValid)
            {
                throw new ModelFormatException();
            }

            return(Ok(_tapeService.UpdateTape(tape, (int)id)));
        }
Beispiel #6
0
 public IActionResult UpdateTapeById([FromBody] TapeInputModel tape, int tapeId)
 {
     if (!ModelState.IsValid)
     {
         throw new ModelFormatException("Tape was not properly formatted");
     }
     _tapeService.UpdateTape(tape, tapeId);
     return(NoContent());
 }
Beispiel #7
0
        public IActionResult AddNewTape([FromBody] TapeInputModel tape)
        {
            if (!ModelState.IsValid)
            {
                throw new ModelFormatException();
            }

            return(StatusCode(201, _tapeService.AddNewTape(tape)));
        }
Beispiel #8
0
        public TapeDetailsDto AddNewTape(TapeInputModel tape)
        {
            var tapeEntity = Mapper.Map <Tape>(tape);

            _dataBaseContext.Tapes.Add(tapeEntity);
            _dataBaseContext.SaveChanges();
            var tapeDto = Mapper.Map <TapeDetailsDto>(tapeEntity);

            return(tapeDto);
        }
Beispiel #9
0
        public IActionResult CreateTape([FromBody] TapeInputModel tape)
        {
            if (!ModelState.IsValid)
            {
                throw new ModelFormatException("Tape was not properly formatted");
            }
            var newId = _tapeService.CreateTape(tape);

            return(CreatedAtRoute("GetTapeById", new { tapeId = newId }, null));
        }
Beispiel #10
0
 public IActionResult EditTape(int id, [FromBody] TapeInputModel Tape)
 {
     // Check if input model is valid, output all errors if not
     if (!ModelState.IsValid)
     {
         IEnumerable <string> errorList = ModelState.Values.SelectMany(v => v.Errors).Select(x => x.ErrorMessage);
         throw new InputFormatException("Video tape input model improperly formatted.", errorList);
     }
     // Edit tape if input model was valid
     _tapeService.EditTape(id, Tape);
     return(NoContent());
 }
Beispiel #11
0
        public IActionResult CreateTape([FromBody] TapeInputModel Tape)
        {
            // Check if input model is valid, output all errors if not
            if (!ModelState.IsValid)
            {
                IEnumerable <string> errorList = ModelState.Values.SelectMany(v => v.Errors).Select(x => x.ErrorMessage);
                throw new InputFormatException("Video tape input model improperly formatted.", errorList);
            }
            // Create new tape if input model was valid
            int id = _tapeService.CreateTape(Tape);

            return(CreatedAtRoute("GetTapeById", new { id }, null));
        }
Beispiel #12
0
        /// <summary>
        /// Updates tape, throws resource not found exception if no tape is associated to Id
        /// </summary>
        /// <param name="Id">Id associated with tape in system to update</param>
        /// <param name="Tape">New information on tape to swap old information out for</param>
        public void EditTape(int Id, TapeInputModel Tape)
        {
            var oldTape = _tapeRepository.GetAllTapes().FirstOrDefault(t => t.Id == Id);

            if (oldTape == null)
            {
                throw new ResourceNotFoundException($"Video tape with id {Id} was not found.");
            }
            else
            {
                _tapeRepository.EditTape(Id, Tape);
            }
        }
Beispiel #13
0
        public async Task <IActionResult> Update([FromRoute] int id, [FromBody] TapeInputModel inputModel)
        {
            var tape = await TapesService.GetSingle(id);

            tape.Title             = inputModel.Title;
            tape.Type              = inputModel.Type;
            tape.ReleaseDate       = inputModel.ReleaseDate;
            tape.Eidr              = inputModel.Eidr;
            tape.DirectorFirstName = inputModel.DirectorFirstName;
            tape.DirectorLastName  = inputModel.DirectorLastName;

            TapesService.Update(tape);
            return(NoContent());
        }
Beispiel #14
0
        public IActionResult Create([FromBody] TapeInputModel inputModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var tape = new Tape
            {
                Title             = inputModel.Title,
                Type              = inputModel.Type,
                ReleaseDate       = inputModel.ReleaseDate,
                Eidr              = inputModel.Eidr,
                DirectorFirstName = inputModel.DirectorFirstName,
                DirectorLastName  = inputModel.DirectorLastName,
            };

            TapesService.Create(tape);
            return(CreatedAtAction(nameof(GetSingle), new { id = tape.Id }, tape.ToDto()));
        }
Beispiel #15
0
        public TapeDetailsDto UpdateTape(TapeInputModel tape, int id)
        {
            var tapeEntity = (from t in _dataBaseContext.Tapes where id == t.Id select t).FirstOrDefault();

            if (tapeEntity == null)
            {
                throw new ResourceNotFoundException();
            }

            // update the tape with the info given
            tapeEntity.Eidr         = tape.Eidr;
            tapeEntity.Title        = tape.Title;
            tapeEntity.Type         = tape.Type;
            tapeEntity.DirectorName = tape.DirectorName;
            tapeEntity.ReleaseDate  = tape.ReleaseDate;
            // database specific
            tapeEntity.DateModified = DateTime.Now;
            _dataBaseContext.SaveChanges();
            // return the updated tape
            var tapeDetailsDto = Mapper.Map <TapeDetailsDto>(tapeEntity);

            return(tapeDetailsDto);
        }
Beispiel #16
0
 public TapeDetailsDto UpdateTape(TapeInputModel tape, int id)
 {
     return(_tapeRepository.UpdateTape(tape, id));
 }
Beispiel #17
0
 public TapeDetailsDto AddNewTape(TapeInputModel tape)
 {
     return(_tapeRepository.AddNewTape(tape));
 }
        //Update a valid tape
        //Throws exception if tape id is invalid
        public void UpdateTape(TapeInputModel tape, int tapeId)
        {
            var updateTape = IsValidId(tapeId);

            _tapeRepository.UpdateTapeById(Mapper.Map <Tape>(tape), tapeId);
        }
Beispiel #19
0
 /// <summary>
 /// Creates new tape
 /// </summary>
 /// <param name="Tape">new tape to add</param>
 /// <returns>the Id of new tape</returns>
 public int CreateTape(TapeInputModel Tape) =>
 _tapeRepository.CreateTape(Tape);