Пример #1
0
        public async Task <ResourceResult <EmployeeResource> > UpsertAsync(EmployeeResource resource)
        {
            var result = new ResourceResult <EmployeeResource>();

            // Beautify before validation and make validation more succesfull
            BeautifyResource(resource);

            // save beautify effect effect
            result.Resource = resource;

            // Apply simple validation on attribute level
            ValidateAttributes(resource, result.Errors);

            // Apply complex business rules validation
            ValidateBusinessRules(resource, result.Errors);

            // Save is only usefull when error free
            if (result.Errors.Count == 0)
            {
                // Convert resource to entity
                var entity = mapper.Map <Employee>(resource);

                // save entity
                await EntityContext.UpsertAsync(entity);

                // convert save result back to resource and get database created values like auto incremental field and timestamps.
                result.Resource = mapper.Map <EmployeeResource>(entity);
            }

            return(result);
        }
Пример #2
0
        public async Task <ResourceResult <EmployeeResource> > DeleteAsync(Guid id)
        {
            var result = new ResourceResult <EmployeeResource>();

            // Check if resource still exists
            result.Resource = await FindAsync(id);

            if (result.Resource != null)
            {
                // Check if delete is allowed
                ValidateDelete(result.Resource, result.Errors);

                // Delete only if allowed
                if (result.Errors.Count == 0)
                {
                    var entity = mapper.Map <Employee>(result.Resource);

                    await EntityContext.DeleteAsync(entity);
                }
            }

            return(result);
        }