Exemplo n.º 1
0
        public async Task <ActionResult> Edit(EditEventViewModel model, HttpPostedFileBase image)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new
                {
                    success = false,
                    errors = ModelState.Keys.SelectMany(k => ModelState[k].Errors)
                             .Select(m => m.ErrorMessage).ToArray()
                }));
            }

            try
            {
                string newImagePath = string.Empty;
                string newImageUrl  = string.Empty;
                string oldImageUrl  = string.Empty;

                var update = await _eventService.GetAsync(model.Event.Id);

                if (image != null && image.ContentLength > 0)
                {
                    newImageUrl = GetImageUrl(image, out newImagePath);
                    oldImageUrl = update.ImageURL;
                }

                update.Date        = model.Event.Date.Date + model.Event.Time.TimeOfDay;
                update.Description = model.Event.Description;
                update.ImageURL    = string.IsNullOrEmpty(newImageUrl) ? update.ImageURL : newImageUrl;
                update.LayoutId    = model.Event.LayoutId;
                update.Title       = model.Event.Title;
                await _eventService.UpdateAsync(update);

                if (!string.IsNullOrEmpty(newImageUrl))
                {
                    image.SaveAs(newImagePath);
                }
            }
            catch (FaultException <ServiceValidationFaultDetails> exception)
            {
                string error = string.Empty;

                if (exception.Message.Equals("Invalid date", StringComparison.OrdinalIgnoreCase))
                {
                    error = ProjectResources.ResourceErrors.EventInvalidDateError;
                }

                if (exception.Message.Equals("Attempt of updating event with a date in the past", StringComparison.OrdinalIgnoreCase))
                {
                    error = ProjectResources.ResourceErrors.PastDateError;
                }

                if (exception.Message.Equals("Not allowed to update layout. Event has locked seats", StringComparison.OrdinalIgnoreCase))
                {
                    error = ProjectResources.ResourceErrors.EventLayoutChangeError;
                }

                return(Json(new
                {
                    success = false,
                    errors = new string[] { error }
                }));
            }
            catch (FaultException exception)
            {
                return(Json(new
                {
                    success = false,
                    error = exception.Message
                }));
            }

            return(Json(new
            {
                success = true
            }));
        }