Example #1
0
        public int Add(DTOResource dtoResource)
        {
            Resource entityResource = ResourceMapper.GetEntityResource(dtoResource);

            entityResource.Id = UnitOfWork.GetResourceId();

            entityResource.Email = $"{entityResource.Name}.{entityResource.LastName}@reti.it";

            if (dtoResource.LastName.Replace(" ", "").Length >= 5)
            {
                entityResource.Username = dtoResource.LastName.Replace(" ", "").Substring(0, 5).ToLower();
            }
            else
            {
                entityResource.Username = dtoResource.LastName.Replace(" ", "").ToLower();
            }

            if (dtoResource.Name.Replace(" ", "").Length >= 2)
            {
                entityResource.Username += dtoResource.Name.Replace(" ", "").Substring(0, 2).ToLower();
            }
            else
            {
                entityResource.Username += dtoResource.Name.Replace(" ", "").ToLower();
            }

            int ResourceWithSameUsername = UnitOfWork.ResourceRepository.GetAll().Where(res => res.Username.Substring(0, res.Username.Length - 1) == entityResource.Username).Count();

            entityResource.Username += ResourceWithSameUsername + 1;

            UnitOfWork.ResourceRepository.Add(entityResource);
            UnitOfWork.Commit();

            return(entityResource.Id);
        }