public async Task <IActionResult> CreateRobot(RobotInputModel model) { bool imageNotNull = model.Image != null; bool wrongType = false; if (imageNotNull) { var fileType = model.Image.ContentType.Split('/')[1]; wrongType = GlobalConstants.ImageExtensions.Contains(fileType); } if (!ModelState.IsValid) { return(this.View(model)); } if (!wrongType && imageNotNull) { this.ViewData[GlobalConstants.Error] = GlobalConstants.WrongFileType; return(this.View(model)); } var id = await this.robotService.CreateRobot(model); return(RedirectToAction("Details", "Robots", new { id })); }
public void CreateRobot_With_No_Image_Should_Succeed() { var user = new User() { Id = "1", Name = "Gosho", UserName = "******" }; userManager.CreateAsync(user).GetAwaiter(); this.Context.SaveChanges(); var robotInputModel = new RobotInputModel { User = user.UserName, Image = null, Axes = 2, Name = "myRobot", SerialNumber = "24242" }; var result = this.robotService.CreateRobot(robotInputModel).GetAwaiter().GetResult(); var robot = this.Context.Robots.FirstOrDefault(); result.Should().NotBeNull(); robot.Should().NotBeNull() .And.Subject.Should().BeEquivalentTo(new { Id = result, ImageUrl = GlobalConstants.NoImageAvailableUrl, Name = robot.Name }, options => options.ExcludingMissingMembers()); }
public async Task <string> CreateRobot(RobotInputModel model) { var cloudinary = SetCloudinary(); var url = await UploadImage(cloudinary, model.Image, model.Name); var robot = Mapper.Map <Robot>(model); robot.User = await this.UserManager.FindByNameAsync(model.User); robot.ImageUrl = url ?? GlobalConstants.NoImageAvailableUrl; this.Context.Robots.Add(robot); await this.Context.SaveChangesAsync(); return(robot.Id); }