Exemple #1
0
        public FieldResponse CreateField(Guid entityId, InputField inputField, bool transactional = true)
        {
            FieldResponse response = new FieldResponse
            {
                Success = true,
                Message = "The field was successfully created!",
            };

            Field field = null;

            try
            {
                IStorageEntity storageEntity = EntityRepository.Read(entityId);

                if (storageEntity == null)
                {
                    response.Timestamp = DateTime.UtcNow;
                    response.Success = false;
                    response.Message = "Entity with such Id does not exist!";
                    return response;
                }

                if (inputField.Id == null || inputField.Id == Guid.Empty)
                    inputField.Id = Guid.NewGuid();

                Entity entity = storageEntity.MapTo<Entity>();

                response.Errors = ValidateField(entity, inputField, false);

                field = inputField.MapTo<Field>();

                if (response.Errors.Count > 0)
                {
                    response.Object = field;
                    response.Timestamp = DateTime.UtcNow;
                    response.Success = false;
                    response.Message = "The field was not created. Validation error occurred!";
                    return response;
                }

                entity.Fields.Add(field);

                IStorageEntity editedEntity = entity.MapTo<IStorageEntity>();

                var recRep = Storage.GetRecordRepository();
                var transaction = recRep.CreateTransaction();
                try
                {
                    if (transactional)
                        transaction.Begin();

                    recRep.CreateRecordField(entity.Name, field.Name, field.GetDefaultValue());

                    bool result = EntityRepository.Update(editedEntity);
                    if (!result)
                    {
                        response.Timestamp = DateTime.UtcNow;
                        response.Success = false;
                        response.Message = "The field was not created! An internal error occurred!";
                        return response;
                    }

                    if (transactional)
                        transaction.Commit();
                }
                catch
                {
                    if (transactional)
                        transaction.Rollback();
                    throw;
                }

            }
            catch (Exception e)
            {
                response.Success = false;
                response.Object = field;
                response.Timestamp = DateTime.UtcNow;
            #if DEBUG
                response.Message = e.Message + e.StackTrace;
            #else
                response.Message = "The field was not created. An internal error occurred!";
            #endif
                return response;
            }

            response.Object = field;
            response.Timestamp = DateTime.UtcNow;

            return response;
        }
Exemple #2
0
        public FieldResponse UpdateField(Entity entity, InputField inputField)
        {
            FieldResponse response = new FieldResponse
            {
                Success = true,
                Message = "The field was successfully updated!",
            };

            Field field = null;

            try
            {
                response.Errors = ValidateField(entity, inputField, true);

                field = inputField.MapTo<Field>();

                if (response.Errors.Count > 0)
                {
                    response.Object = field;
                    response.Timestamp = DateTime.UtcNow;
                    response.Success = false;
                    response.Message = "The field was not updated. Validation error occurred!";
                    return response;
                }

                Field fieldForDelete = entity.Fields.FirstOrDefault(f => f.Id == field.Id);
                if (fieldForDelete.Id == field.Id)
                    entity.Fields.Remove(fieldForDelete);

                entity.Fields.Add(field);

                IStorageEntity updatedEntity = entity.MapTo<IStorageEntity>();
                bool result = EntityRepository.Update(updatedEntity);
                if (!result)
                {
                    response.Timestamp = DateTime.UtcNow;
                    response.Success = false;
                    response.Message = "The field was not updated! An internal error occurred!";
                    return response;
                }

            }
            catch (Exception e)
            {
                response.Success = false;
                response.Object = field;
                response.Timestamp = DateTime.UtcNow;
            #if DEBUG
                response.Message = e.Message + e.StackTrace;
            #else
                response.Message = "The field was not updated. An internal error occurred!";
            #endif
                return response;
            }

            response.Object = field;
            response.Timestamp = DateTime.UtcNow;

            return response;
        }
Exemple #3
0
        public FieldResponse CreateField(Guid entityId, InputField inputField, bool transactional = true)
        {
            FieldResponse response = new FieldResponse
            {
                Success = true,
                Message = "The field was successfully created!",
            };

            Field field = null;

            try
            {
                var entityResponse = ReadEntity(entityId);

                if (!entityResponse.Success)
                {
                    response.Timestamp = DateTime.UtcNow;
                    response.Success = false;
                    response.Message = entityResponse.Message;
                    return response;
                }
                else if (entityResponse.Object == null)
                {
                    response.Timestamp = DateTime.UtcNow;
                    response.Success = false;
                    response.Message = "Entity with such Id does not exist!";
                    return response;
                }
                Entity entity = entityResponse.Object;

                if (inputField.Id == null || inputField.Id == Guid.Empty)
                    inputField.Id = Guid.NewGuid();

                response.Errors = ValidateField(entity, inputField, false);

                field = inputField.MapTo<Field>();

                if (response.Errors.Count > 0)
                {
                    response.Object = field;
                    response.Timestamp = DateTime.UtcNow;
                    response.Success = false;
                    response.Message = "The field was not created. Validation error occurred!";
                    return response;
                }

                entity.Fields.Add(field);

                DbEntity editedEntity = entity.MapTo<DbEntity>();

                using (DbConnection con = DbContext.Current.CreateConnection())
                {
                    con.BeginTransaction();

                    try
                    {
                        bool result = DbContext.Current.EntityRepository.Update(editedEntity);
                        if (!result)
                        {
                            response.Timestamp = DateTime.UtcNow;
                            response.Success = false;
                            response.Message = "The field was not created! An internal error occurred!";
                            return response;
                        }

                        DbContext.Current.RecordRepository.CreateRecordField(entity.Name, field);

                        con.CommitTransaction();
                    }
                    catch
                    {
                        con.RollbackTransaction();
                        throw;
                    }
                }

            }
            catch (Exception e)
            {
                Cache.ClearEntities();

                response.Success = false;
                response.Object = field;
                response.Timestamp = DateTime.UtcNow;
            #if DEBUG
                response.Message = e.Message + e.StackTrace;
            #else
                response.Message = "The field was not created. An internal error occurred!";
            #endif
                return response;
            }

            Cache.ClearEntities();

            response.Object = field;
            response.Timestamp = DateTime.UtcNow;

            return response;
        }