Beispiel #1
0
        public async Task <ActionResult> Create(RentingBindModel model)
        {
            if (!ModelState.IsValid)
            {
                model.Username = null; model.Title = null; model.MovieId = null;
                return(View(model));
            }
            try
            {
                var user = _userService.GetUserByUserName(model.Username);
                var copy = await _copyService.GetAvailableCopyById((int)model.MovieId);

                if (user == null)
                {
                    ModelState.AddModelError("", "Δεν υπάρχει αυτός ο πελάτης!");
                    model.Username = null;
                    return(View(model));
                }

                if (copy == null)
                {
                    ModelState.AddModelError("", "Δεν υπάρχει αυτή η ταινία!");
                    model.Title = null; model.MovieId = 0;
                    return(View(model));
                }

                var renting = _mapper.Map <Renting>(model);
                renting.RentingDate         = DateTime.UtcNow;
                renting.ScheduledReturnDate = DateTime.UtcNow.AddDays(7);
                renting.ReturnDate          = DateTime.UtcNow.AddDays(7);
                renting.IsActive            = true;

                await _rentingService.AddRenting(renting, user, copy);

                _logger.Writer.Information("Renting was created for User: {Username} and Movie: {Title} with Copy: {CopyID}", user.UserName, copy.Movie.Title, copy.Id);
            }
            catch (Exception e)
            {
                _logger.Writer.Fatal(e, "Renting could not be created.");
                model.Username = null; model.Title = null; model.MovieId = 0;
                return(View(model));
            }

            return(RedirectToAction("index", "rentings"));
        }
Beispiel #2
0
        // GET: /rentings/create
        public async Task <ActionResult> Create(string customer, int?movieId)
        {
            try
            {
                string title    = null;
                string username = null;

                if (movieId != null)
                {
                    var copy = await _copyService.GetAvailableCopyById((int)movieId);

                    if (copy != null)
                    {
                        title = copy.Movie.Title;
                    }
                }

                if (!String.IsNullOrEmpty(customer))
                {
                    var user = _userService.GetUserByUserName(customer);
                    if (user != null)
                    {
                        username = user.UserName;
                    }
                }
                // TODO mapping(?)
                var rentingBindModel = new RentingBindModel(username, title, movieId);

                return(View(rentingBindModel));
            }
            catch (Exception e)
            {
                _logger.Writer.Fatal(e, "/rentings/create View could not be loaded.");
                return(View("Error"));
            }
        }