예제 #1
0
        public static EntityVm CreateViewModel(EntityRecord record, LoadMode mode,
                                               IStorageProvider storageProvider)
        {
            if (record == null)
            {
                throw new ArgumentNullException("record");
            }

            var result = new EntityVm {
                Id = record.Id
            };

            if (mode == LoadMode.Compact || mode == LoadMode.Full)
            {
                result.Name         = record.Name;
                result.Abbreviation = record.Name.GetAbbreviation(2);
                result.Picture      = FileUtil.GetPictureUrl(record.Picture, storageProvider);
                result.Addresses    = record.AddressRecords
                                      .Where(ar => !ar.IsDeleted)
                                      .Select(CreateViewModel)
                                      .ToArray();
            }

            if (mode == LoadMode.Full)
            {
                result.Type        = (EntityType)record.Type;
                result.Description = record.Description;
                result.Contacts    = record.ContactRecords
                                     .Where(c => !c.IsDeleted)
                                     .Select(CreateViewModel)
                                     .ToArray();
            }

            return(result);
        }
예제 #2
0
        public static string DisplayNumber(this EntityVm venue, EventMetadataVm eventMeta)
        {
            var number = eventMeta.VenueTitleFormatType == VenueTitleFormatType.NameAndNumber
                ? eventMeta.Venues.IndexOf(venue) + 1
                : (int?)null;
            var result = number.HasValue ? string.Format("{0}. ", number) : string.Empty;

            return(result);
        }
예제 #3
0
        public static void UpdateByViewModel(EntityRecord record, EntityVm entityVm)
        {
            record.DateModified = DateTime.UtcNow;

            record.Type        = (byte)entityVm.Type;
            record.Name        = entityVm.Name.TrimIt().StripTags();
            record.Picture     = entityVm.Picture.TrimIt().StripTags();
            record.Description = entityVm.Description.TrimIt().StripTags();
        }
예제 #4
0
 public bool IsNameUnique(EntityVm entityVm)
 {
     return(!_entityRepository
            .Table
            .Any(e =>
                 e.Type == (byte)entityVm.Type &&
                 e.Id != entityVm.Id &&
                 e.Name == entityVm.Name &&
                 !e.IsDeleted));
 }
예제 #5
0
        public ActionResult Validate(string propName, EntityVm model)
        {
            var errors = _validator.ValidateEntity(model);

            if (errors.ContainsPropertyError(propName))
            {
                HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new ErrorResultVm(errors.PropertyErrors(propName))));
            }

            return(Json(true));
        }
예제 #6
0
        public static void UpdateByViewModelPicture(EntityRecord record, EntityVm entityVm,
                                                    IStorageProvider storageProvider)
        {
            var previousPictureUrl = FileUtil.GetPictureUrl(record.Picture, storageProvider);

            if (previousPictureUrl != entityVm.Picture)
            {
                record.Picture = FileUtil.ProcessUploadedPicture(record.Picture, entityVm.Picture,
                                                                 Path.Combine("entity", record.Id.ToString(CultureInfo.InvariantCulture)),
                                                                 storageProvider);
            }
        }
예제 #7
0
        public ActionResult SaveEntity(EntityVm entityVm)
        {
            var errors = _validator.ValidateEntity(entityVm);

            if (errors.Count > 0)
            {
                HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new ErrorResultVm(errors)));
            }

            var result = _entityService.SaveEntity(entityVm);

            return(Json(result));
        }
예제 #8
0
        public EntityVm Init(long userId, long?pin)
        {
            var toRet = new EntityVm
            {
                ActionMode = Enumerations.ActionMode.Add,
                Entity     = new Entity {
                    Status = "A"
                }
            };

            if (pin != null)
            {
                var entity = LoadSingle(userId, Convert.ToInt64(pin));
                toRet.Entity = entity;

                toRet.ActionMode = Enumerations.ActionMode.Edit;
            }

            return(toRet);
        }
예제 #9
0
        public EntityVm Save(long userId, EntityVm toSave)
        {
            var entity = toSave.Entity;

            PreSave(userId, ref entity, toSave.ActionMode);
            toSave.Entity = entity;

            switch (toSave.ActionMode)
            {
            case Enumerations.ActionMode.Add:
                toSave.Entity = Create(userId, toSave.Entity);
                break;

            case Enumerations.ActionMode.Edit:
                toSave.Entity = Edit(userId, toSave.Entity);
                break;
            }

            return(Init(userId, toSave.Entity.Pin));
        }
예제 #10
0
        private static void SaveVenueDetail(EventMetadataRecord eventMeta, EntityRecord venue, EntityVm venueVm)
        {
            // ReSharper disable once AccessToForEachVariableInClosure
            var entityDetail = eventMeta
                               .EventEntityDetailRecords
                               .FirstOrDefault(eedr => eedr.EntityRecord.Id == venueVm.Id);

            if (venueVm.EventDetail != null)
            {
                entityDetail = entityDetail ?? new EventEntityDetailRecord
                {
                    EntityRecord        = venue,
                    EventMetadataRecord = eventMeta
                };

                EntityService
                .ViewModelFactory
                .UpdateByViewModel(entityDetail, venueVm.EventDetail);

                if (entityDetail.Id == 0)
                {
                    eventMeta.EventEntityDetailRecords.Add(entityDetail);
                }

                entityDetail.IsDeleted = venueVm.Destroy;
            }
            else if (entityDetail != null)
            {
                entityDetail.IsDeleted = true;
            }
        }
예제 #11
0
 public void Save(EntityVm model)
 {
     _blObj.Save(ApiCommon.CurrentUserId, model);
 }
예제 #12
0
        public IList <ValidationError> ValidateEntity(EntityVm model)
        {
            var access = _entityService.GetEntitiesAccess();

            if (access != AccessType.AllowEdit)
            {
                throw new SecurityException("Validation is prohibited due to security restrictions");
            }

            var result = new List <ValidationError>();

            var nameProperty = model.GetPropertyName(p => p.Name);

            if (string.IsNullOrEmpty(model.Name))
            {
                result.Add(new ValidationError(nameProperty, T(EntityTypeName + " name can not be empty.").Text));
            }
            else if (model.Name.Length > 255)
            {
                result.Add(new ValidationError(
                               nameProperty,
                               T(EntityTypeName + " name can not be longer than 255 characters.").Text));
            }
            else if (!_entityService.IsNameUnique(model))
            {
                result.Add(new ValidationError(nameProperty, T(EntityTypeName + " name must be unique.").Text));
            }

            var pictureProperty = model.GetPropertyName(p => p.Picture);

            if (!string.IsNullOrEmpty(model.Picture))
            {
                if (model.Picture.Length > 255)
                {
                    result.Add(new ValidationError(
                                   pictureProperty,
                                   T("Picture url can not be longer than 255 characters.").Text));
                }
            }

            var descriptionProperty = model.GetPropertyName(p => p.Description);

            if (!string.IsNullOrEmpty(model.Description))
            {
                if (model.Description.Length > 3000)
                {
                    result.Add(new ValidationError(
                                   descriptionProperty,
                                   T("Description can not be longer than 3000 characters.").Text));
                }
            }

            var addressesProperty = model.GetPropertyName(p => p.Addresses);
            var addresses         = model.Addresses != null
                ? model.Addresses.Where(v => !v.Destroy).ToArray()
                : new AddressVm[]
            {
            };

            if (model.Type == EntityType.Venue &&
                addresses.Length == 0)
            {
                result.Add(new ValidationError(
                               addressesProperty,
                               T("At least one address has to be added to the venue.").Text));
            }

            for (var i = 0; i < addresses.Length; i++)
            {
                var addressVm = addresses[i];
                result.AddRange(ValidateAddress(
                                    addressVm,
                                    string.Format("{0}[{1}].", addressesProperty, i + 1)));
            }

            var contactsProperty = model.GetPropertyName(p => p.Contacts);
            var contacts         = model.Contacts != null
                ? model.Contacts.Where(v => !v.Destroy).ToArray()
                : new ContactVm[]
            {
            };

            for (var i = 0; i < contacts.Length; i++)
            {
                var contactVm = contacts[i];
                result.AddRange(ValidateContact(
                                    contactVm,
                                    string.Format("{0}[{1}].", contactsProperty, i + 1)));
            }

            return(result);
        }
예제 #13
0
        public EntityVm SaveEntity(EntityVm entityVm)
        {
            if (entityVm == null)
            {
                throw new ArgumentNullException("entityVm");
            }

            var entity = _entityRepository.Get(entityVm.Id) ?? new EntityRecord
            {
                SmartWalkUserRecord = CurrentUserRecord,
                DateCreated         = DateTime.UtcNow
            };

            var access = entity.GetAccess(Services.Authorizer, CurrentUserRecord);

            if (access != AccessType.AllowEdit)
            {
                throw new SecurityException("Can't edit entity.");
            }

            if (entity.IsDeleted)
            {
                throw new InvalidOperationException("Can't edit deleted entity.");
            }

            ViewModelFactory.UpdateByViewModel(entity, entityVm);

            foreach (var contactVm in entityVm.Contacts)
            {
                // ReSharper disable once AccessToForEachVariableInClosure
                var contact = contactVm.Id > 0
                    ? entity.ContactRecords.FirstOrDefault(cr => cr.Id == contactVm.Id)
                    : null;
                if (contact == null && !contactVm.Destroy)
                {
                    contact = new ContactRecord {
                        EntityRecord = entity
                    };
                    entity.ContactRecords.Add(contact);
                }

                if (contact != null)
                {
                    ViewModelFactory.UpdateByViewModel(contact, contactVm);
                }
            }

            foreach (var addressVm in entityVm.Addresses)
            {
                // ReSharper disable once AccessToForEachVariableInClosure
                var address = addressVm.Id > 0
                    ? entity.AddressRecords.FirstOrDefault(ar => ar.Id == addressVm.Id)
                    : null;
                if (address == null && !addressVm.Destroy)
                {
                    address = new AddressRecord {
                        EntityRecord = entity
                    };
                    entity.AddressRecords.Add(address);
                }

                if (address != null)
                {
                    ViewModelFactory.UpdateByViewModel(address, addressVm);
                }
            }

            if (entity.Id == 0)
            {
                _entityRepository.Create(entity);
            }

            // handling uploaded pictures after event got an Id
            ViewModelFactory.UpdateByViewModelPicture(entity, entityVm, _storageProvider);

            _entityRepository.Flush();

            var result = ViewModelFactory.CreateViewModel(entity, LoadMode.Full, _storageProvider);

            return(result);
        }
예제 #14
0
 public static AddressVm FirstAddress(this EntityVm entity)
 {
     return(entity.Addresses.FirstOrDefault(ad => ad.Address != null));
 }
예제 #15
0
 public static bool HasAddresses(this EntityVm entity)
 {
     return(entity.Addresses.Any(ad => ad.Address != null));
 }