Example #1
0
        /// <summary>
        /// This method assumes that RemoteRepo.Load() was already called.
        /// </summary>
        /// <param name="app"></param>
        /// <returns></returns>
        public List<RemoteVsLocalFile> GroupFilesByName
            (string localFoldrPath, IRepository<SyncableFileRemote> repo, SyncDirection syncDirection)
        {
            var list   = new List<RemoteVsLocalFile>();
            var locals = _fileSeeker.GetFiles(localFoldrPath);


            foreach (var loc in locals)
            {
                var rem = repo.One(x => x.Name == loc.Name);
                list.Add(RemVsLoc(loc, rem, syncDirection));
            }


            foreach (var rem in repo.Any(r
                => !list.Has(l => l.Filename == r.Name)))
            {
                var loc = locals.One(x => x.Name == rem.Name);
                list.Add(RemVsLoc(loc, rem, syncDirection));
            }


            //  if no files in folder, or no folder at all,
            //    - assume that it's a first run : download all
            //
            if (locals.Count == 0)
                list.ForEach(x => x.DoNext(Target.Local, FileTask.Create));


            return list;
        }
        public TransactionRequestModule(IRepository<TransactionRequest> transactionRequestRepo)
        {
            Get["/Transactions/Request/{requestId}"] = param =>
            {
                var requestId = new Guid(param.requestId);

                if (!transactionRequestRepo.Any(x => x.RequestId == requestId && x.ExpirationDate > DateTime.UtcNow))
                    return Response.AsJson(new { Error = "Request expired" });

                if (transactionRequestRepo.Any(x => x.RequestId == requestId && x.UserState == ApiCommitRequestUserState.Cancelled))
                    return Response.AsJson(new { Error = "Request cancelled by user" });

                if (transactionRequestRepo.Any(x => x.RequestId == requestId && x.ExpirationDate > DateTime.UtcNow))
                    return Response.AsJson(new { Success = "Request valid" });

                return Response.AsJson(new { Error = "Technical error" });
            };
        }
        public UserAliasModule(IBus bus, IRepository<ClientUserAlias> userAliasRepository, IRepository<User> usereRepository)
        {
            Get["/UserAliases/{clientId:guid}"] = _ =>
            {
                var clientId = (Guid)_.clientId;
                var aliases = userAliasRepository.Where(x => x.Client.Id == clientId);
                return Response.AsJson(Mapper.Map<IEnumerable<ClientUserAlias>, IEnumerable<UserAliasDto>>(aliases));
            };

            //TODO: Error checking for file type. Allow csv only
            Post["/UserAliases/ImportUsers/FilesUpload/{clientId:guid}"] = _ =>
            {
                var clientId = (Guid)_.clientId;
                var filesUploaded = Request.Files;
                var files = new List<FileUploadDto>();
                var clientImportUsers = new List<UserAliasDto>();

                foreach (var httpFile in filesUploaded)
                {
                    //if (httpFile.ContentType != "text/csv")
                    //{
                    //    // Response for file upload component
                    //    files.Add(new FileUploadDto
                    //    {
                    //        name = httpFile.Name,
                    //        size = Convert.ToInt32(httpFile.Value.Length),
                    //        error = "File type not allowed"
                    //    });

                    //    break;
                    //};

                    // CSV to object
                    using (var reader = new StreamReader(httpFile.Value))
                    {
                        var contents = reader.ReadToEnd().Split('\n');
                        var csv = from line in contents select line.Split(',').ToArray();

                        clientImportUsers.AddRange(csv.Skip(1).TakeWhile(r => r.Length > 1 && r.Last().Trim().Length > 0)
                            .Select(row => new UserAliasDto
                            {
                                UuId = row[0],
                                FirstName = row[1],
                                LastName = row[2],
                                UserName = row[3].Replace("\r", ""),
                                ClientId = clientId
                            }));

                        foreach (var clientImportUser in clientImportUsers)
                        {
                            var exists = userAliasRepository.Any(x => x.Client.Id == clientId && x.UserName.Trim().ToLower() == clientImportUser.UserName.Trim().ToLower());
                            if (exists)
                                throw new LightstoneAutoException("{0} already exists".FormatWith(clientImportUser.UserName));
                            var entity = Mapper.Map(clientImportUser, new ClientUserAlias());
                            bus.Publish(new CreateUpdateEntity(entity, "Create"));
                        }
                    }

                    // Response for file upload component
                    files.Add(new FileUploadDto
                    {
                        name = httpFile.Name,
                        size = Convert.ToInt32(httpFile.Value.Length),
                        thumbnailUrl = "http://icons.iconarchive.com/icons/custom-icon-design/pretty-office-2/72/success-icon.png",
                        deleteType = "DELETE"
                    });
                }

                var fileResponseJsonObject = new JObject(new JProperty("files", JsonConvert.SerializeObject(files)));

                return Response.AsJson(fileResponseJsonObject);
            };

            Post["/UserAliases/LinkAlias"] = _ =>
            {
                var dto = this.Bind<AliasDto>();
                var command = new LinkUserAliasCommand(dto.AliasId, dto.CustomerId, dto.UserId);
                bus.Publish(command);

                return Response.AsJson("Saved!");
            };
        }