Ejemplo n.º 1
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());
 }
Ejemplo n.º 2
0
        public IActionResult InitializeUsersAndBorrows()
        {
            // We do not initialize unless database is empty for safety reasons
            if (_userService.GetAllUsers().Count > 0)
            {
                return(BadRequest(new ExceptionModel {
                    StatusCode = (int)HttpStatusCode.BadRequest,
                    Message = "Users have already been initialized in some form"
                }));
            }
            // We do not initialize users unless tapes have been initialized due to borrow records
            if (_tapeService.GetAllTapes().Count == 0)
            {
                return(BadRequest(new ExceptionModel {
                    StatusCode = (int)HttpStatusCode.BadRequest,
                    Message = "Tapes need to be initialized before users. Call [POST] /tapes/initialize before."
                }));
            }
            // Otherwise add users from initialization file
            using (StreamReader r = StreamReaderFactory.GetStreamReader("../Resources/usersAndBorrows.json")) {
                string  json      = r.ReadToEnd();
                dynamic usersJSON = JsonConvert.DeserializeObject(json);
                foreach (var userJSON in usersJSON)
                {
                    // Generate input model from json for user
                    UserInputModel user = SeedingUtils.ConvertJsonToUserInputModel(userJSON);
                    // 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("User in initialization file improperly formatted.", errorList);
                    }
                    // Create new user if input model was valid
                    Console.WriteLine($"adding user and user records for user {userJSON.id} of {usersJSON.Count}");
                    int userId = _userService.CreateUser(user);

                    SeedingUtils.CreateTapesForUser(userJSON, _tapeService);
                }
            }
            return(NoContent());
        }