コード例 #1
0
        public async Task <IActionResult> RemoveManagerFromLogbook(LogbookViewModel model)
        {
            try
            {
                var logbookId = model.Id;
                var managerId = model.ManagerId;


                var logbook = await this.logbookService.GetLogbookById(logbookId);

                var manager = await this.userService.GetUserByIdAsync(managerId);

                await this.logbookService.RemoveManagerFromLogbookAsync(managerId, logbookId);

                return(Ok(string.Format(WebConstants.SuccessfullyRemovedManagerFromLogbook, manager.UserName, logbook.Name)));
            }
            catch (NotFoundException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (ArgumentException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (Exception ex)
            {
                log.Error("Unexpected exception occured:", ex);
                return(RedirectToAction("Error", "Home"));
            }
        }
コード例 #2
0
        public async Task ThrowsException()
        {
            var logbookServiceMock = new Mock <ILogbookService>();
            var userServiceMock    = new Mock <IUserService>();
            var imageOptimizerMock = new Mock <IImageOptimizer>();

            var sut = new LogbooksController(logbookServiceMock.Object, userServiceMock.Object, imageOptimizerMock.Object);

            var logbookViewModel = new LogbookViewModel()
            {
                Id             = 1,
                Name           = "Logbook01",
                BusinessUnitId = 1,
                LogbookPicture = null
            };

            logbookServiceMock.Setup(x => x.CreateLogbookAsync(logbookViewModel.Name, logbookViewModel.BusinessUnitId, logbookViewModel.Picture)).ThrowsAsync(new Exception());

            var actionResult = await sut.Create(logbookViewModel);

            var result = (RedirectToActionResult)actionResult;

            Assert.AreEqual("Error", result.ActionName);
            Assert.AreEqual("Home", result.ControllerName);
            Assert.IsInstanceOfType(result, typeof(RedirectToActionResult));
        }
コード例 #3
0
        public ActionResult Logbook(Guid logbookId)
        {
            ViewBag.BackLinkHtml = MenuConstructor.ConstructHtmlBackLink("Logbook", logbookId);
            var logbook    = new LogbookViewModel();
            var logbookDTO = DataAccess.GetLogbook(logbookId);

            logbook.Name        = logbookDTO.Name;
            logbook.Activity    = DataAccess.GetActivity(logbookDTO.DefaultActivityId).Name;
            logbook.LastUpdated = logbookDTO.UpdateDate;
            logbook.LogbookId   = logbookDTO.LogbookId;
            logbook.Entries     = DataAccess.GetLogbookEntries(logbook.LogbookId);
            return(View(logbook));
        }
コード例 #4
0
        public async Task <IActionResult> Update(LogbookViewModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(BadRequest(string.Format(WebConstants.UnableToUpdateLogbook, model.Name)));
            }

            try
            {
                var logbookDTO = await this.logbookService.GetLogbookById(model.Id);

                string imageName = null;

                if (model.LogbookPicture != null)
                {
                    imageName = optimizer.OptimizeImage(model.LogbookPicture, 400, 800);
                }

                if (model.Picture != null)
                {
                    optimizer.DeleteOldImage(model.Picture);
                }

                logbookDTO = await this.logbookService.UpdateLogbookAsync(model.Id, model.Name, model.BusinessUnitId, imageName);

                if (logbookDTO.Name != model.Name)
                {
                    return(BadRequest(string.Format(WebConstants.UnableToUpdateLogbook, model.Name)));
                }

                return(Ok(string.Format(WebConstants.LogbookUpdated, model.Name)));
            }

            catch (NotFoundException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (ArgumentException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (AlreadyExistsException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (Exception ex)
            {
                log.Error("Unexpected exception occured:", ex);
                return(RedirectToAction("Error", "Home"));
            }
        }
コード例 #5
0
        public async Task <IActionResult> Create(LogbookViewModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(BadRequest(WebConstants.EnterValidData));
            }

            try
            {
                string imageName = null;

                if (model.LogbookPicture != null)
                {
                    imageName = optimizer.OptimizeImage(model.LogbookPicture, 400, 800);
                }

                var logbook = await this.logbookService.CreateLogbookAsync(model.Name, model.BusinessUnitId, imageName);

                if (logbook.Name == model.Name)
                {
                    return(Ok(string.Format(WebConstants.LogbookCreated, model.Name)));
                }

                return(BadRequest(string.Format(WebConstants.LogbookNotCreated, model.Name)));
            }

            catch (ArgumentException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (AlreadyExistsException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (Exception ex)
            {
                log.Error("Unexpected exception occured:", ex);
                return(RedirectToAction("Error", "Home"));
            }
        }
コード例 #6
0
        public ActionResult Index(int?year)
        {
            if (!Request.IsPilot())
            {
                return(RedirectToAction("PilotNotFound", "Error"));
            }

            LogbookViewModel model = new LogbookViewModel();

            model.Pilot = Request.Pilot();

            model.Year = DateTime.Now.Year;
            if (year.HasValue)
            {
                if ((DateTime.Now.Year >= year) && (year > 1990))
                {
                    model.Year = year.Value;
                }
            }

            // Custom inline Pilot filtering for allowing maximum performance
            model.Flights = this.db.Flights.Where(f => f.Date.Year >= model.Year - 1 && f.Deleted == null)
                            .Include("Plane").Include("StartedFrom").Include("LandedOn").Include("Pilot").Include("PilotBackseat").Include("Betaler")
                            .Where(f => (f.Pilot != null && f.Pilot.PilotId == model.Pilot.PilotId) ||
                                   (f.PilotBackseat != null && f.PilotBackseat.PilotId == model.Pilot.PilotId) ||
                                   (f.Betaler != null && f.Betaler.PilotId == model.Pilot.PilotId))
                            .OrderByDescending(o => o.Departure)
                            .AsQueryable();

            if (model.Year == DateTime.Now.Year)
            {
                var last12months = DateTime.Now.AddYears(-1);
                model.TrainingBarometer        = GetTrainingBarometer(model.Flights.Where(f => f.Date > last12months));
                model.TrainingBarometerEnabled = true;
            }


            return(this.View(model));
        }
コード例 #7
0
        public async Task ThrowsAlreadyExistsException()
        {
            var logbookServiceMock = new Mock <ILogbookService>();
            var userServiceMock    = new Mock <IUserService>();
            var imageOptimizerMock = new Mock <IImageOptimizer>();

            var sut = new LogbooksController(logbookServiceMock.Object, userServiceMock.Object, imageOptimizerMock.Object);

            var logbookViewModel = new LogbookViewModel()
            {
                Id             = 1,
                Name           = "Logbook01",
                BusinessUnitId = 1,
                LogbookPicture = null
            };

            logbookServiceMock.Setup(x => x.CreateLogbookAsync(logbookViewModel.Name, logbookViewModel.BusinessUnitId, logbookViewModel.Picture)).ThrowsAsync(new AlreadyExistsException());

            var actionResult = await sut.Create(logbookViewModel);

            Assert.IsInstanceOfType(actionResult, typeof(BadRequestObjectResult));
        }
コード例 #8
0
        public async Task ThrowsBadRequestWhenReviewViewModelIsNotValid()
        {
            var logbookServiceMock = new Mock <ILogbookService>();
            var userServiceMock    = new Mock <IUserService>();
            var imageOptimizerMock = new Mock <IImageOptimizer>();

            var sut = new LogbooksController(logbookServiceMock.Object, userServiceMock.Object, imageOptimizerMock.Object);

            var logbookViewModel = new LogbookViewModel()
            {
                Id             = 1,
                Name           = null,
                BusinessUnitId = 1,
                LogbookPicture = null
            };

            logbookServiceMock.Setup(x => x.CreateLogbookAsync(logbookViewModel.Name, logbookViewModel.BusinessUnitId, logbookViewModel.Picture)).ReturnsAsync(TestHelpersLogbookController.TestLogbookDTO01());

            var actionResult = await sut.Create(logbookViewModel);

            Assert.IsInstanceOfType(actionResult, typeof(BadRequestObjectResult));
        }