예제 #1
0
        public async Task <IActionResult> Add(TourViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (await tourDAL.IsExistedTourIdAsync(model.Id))
                    {
                        ModelState.AddModelError("", $"Tour ID '{model.Id.ToUpper()}' is existed");
                    }
                    if (model.FromDate.CompareTo(DateTime.Now) <= 0)
                    {
                        ModelState.AddModelError("", "Start Date must be greater than today");
                    }
                    else if (model.FromDate.CompareTo(DateTime.Parse("9999-12-31 12:00")) > 0)
                    {
                        ModelState.AddModelError("", "Start Date is out of range");
                    }
                    if (model.ToDate.CompareTo(DateTime.Now) <= 0)
                    {
                        ModelState.AddModelError("", "End Date must be greater than today");
                    }
                    else if (model.ToDate.CompareTo(DateTime.Parse("9999-12-31 12:00")) > 0)
                    {
                        ModelState.AddModelError("", "End Dae is out of range");
                    }
                    if (model.FromDate.CompareTo(model.ToDate) >= 0)
                    {
                        ModelState.AddModelError("", "End Date must be greater than Start Date");
                    }
                    if (!await destinationDAL.IsAvailableDestinationAsync(model.Departure))
                    {
                        ModelState.AddModelError("", $"The departure '{model.Departure.ToUpper()}' is not existed or available");
                    }
                    bool duplicated = false;
                    foreach (string id in model.Destinations)
                    {
                        if (id.Equals(model.Departure, StringComparison.OrdinalIgnoreCase))
                        {
                            duplicated = true;
                        }
                        if (!await destinationDAL.IsAvailableDestinationAsync(id))
                        {
                            ModelState.AddModelError("", $"Destination '{id.ToUpper()}' is not existed or available");
                        }
                    }
                    if (duplicated)
                    {
                        ModelState.AddModelError("", "The departure can't also be destinations");
                    }
                    List <string>         availableGuides = new List <string>();
                    IEnumerable <AppUser> guides          = await userManager.GetUsersInRoleAsync("Guide");

                    foreach (AppUser guide in guides)
                    {
                        if (!await userManager.IsLockedOutAsync(guide))
                        {
                            availableGuides.Add(guide.Id);
                        }
                    }
                    foreach (string id in model.Guides)
                    {
                        if (!availableGuides.Contains(id))
                        {
                            ModelState.AddModelError("", $"Guide '{id.ToUpper()}' is not existed or available");
                        }
                    }
                    if (!ModelState.IsValid)
                    {
                        model.DestinationItems = await InitDestinationItemsAsync();

                        model.GuideItems = await InitGuideItemsAsync();

                        return(View("Add", model));
                    }
                    model.Destinations.Insert(0, model.Departure);
                    List <Destination> destinations = new List <Destination>();
                    foreach (string id in model.Destinations)
                    {
                        destinations.Add(new Destination {
                            Id = id
                        });
                    }
                    Tour tour = new Tour
                    {
                        Id           = model.Id.ToUpper(),
                        Name         = model.Name,
                        FromDate     = model.FromDate,
                        ToDate       = model.ToDate,
                        AdultFare    = model.AdultFare,
                        KidFare      = model.KidFare,
                        Description  = model.Description ?? "",
                        MaxGuest     = model.MaxGuest,
                        Transport    = model.Transport ?? "",
                        Destinations = destinations,
                        Guides       = guides.Where(g => model.Guides.Contains(g.Id)),
                        IsActive     = model.IsActive
                    };
                    string img = model.Image ?? "https://ztourist.blob.core.windows.net/others/tour.jpg";
                    if (model.Photo != null && !string.IsNullOrWhiteSpace(model.Photo?.FileName)) // if photo is chosen then copy
                    {
                        string filePath = model.Id + "." + model.Photo.FileName.Substring(model.Photo.FileName.LastIndexOf(".") + 1);
                        img = await blobService.UploadFile("tours", filePath, model.Photo);
                    }
                    if (img != null)
                    {
                        tour.Image = img;
                        if (await tourDAL.AddTourAsync(tour))
                        {
                            return(RedirectToAction(nameof(Details), new { tour.Id }));
                        }
                        else
                        {
                            ModelState.AddModelError("", "Add tour failed");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "Can't upload image");
                    }
                }
                model.DestinationItems = await InitDestinationItemsAsync();

                model.GuideItems = await InitGuideItemsAsync();

                return(View("Add", model));
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                throw;
            }
        }