Ejemplo n.º 1
0
        public static Note SaveNote(this IRepository repo, string content, User user, long id = 0)
        {
            var isNew = id == 0;

            Note note;
            if (isNew)
            {
                note = new Note { AccessToken = Guid.NewGuid() };
            }
            else
            {
                note = repo.Select<Note>()
                    .Include(n => new { n.AccessToken })
                    .FirstOrDefault(n => n.Id == id && n.User.Id == user.Id);

                if (note == null)
                {
                    throw Errors.Note_NotFound;
                }
            }

            if (!string.IsNullOrEmpty(content))
            {
                var i = content.IndexOf("\n", StringComparison.Ordinal);

                if (i > 0)
                {
                    note.Title = content.Substring(0, Math.Min(i, Constants.TitleLength));
                    note.Summary = content.Substring(i, Math.Min(content.Length - i, Constants.SummaryLength));
                }
                else
                {
                    note.Title = content.Substring(0, Math.Min(content.Length, Constants.TitleLength));
                    note.Summary = content.Substring(0, Math.Min(content.Length, Constants.SummaryLength));
                }
            }

            note.User = user;
            note.Content = (content ?? string.Empty).Trim();
            note.Title = note.Title?.Trim();
            note.Summary = note.Summary?.Trim();
            note.LastUpdateDate = DateTime.UtcNow;

            if (isNew)
            {
                repo.Insert(note);
            }
            else
            {
                repo.Update(note);
            }

            return note;
        }
Ejemplo n.º 2
0
        private Login CreateLogin(User user, bool recovery = false)
        {
            var login = new Login
            {
                User = user,
                Token = Guid.NewGuid(),
                IsPasswordRecovery = recovery,
                ExpireDate = DateTime.UtcNow.AddDays(recovery ? Constants.PasswordRecoveryValidDays : Constants.LoginTokenValidDays)
            };

            _repository.Insert(login);

            return login;
        }
Ejemplo n.º 3
0
        public Response Signup(SignupRequest request)
        {
            var newUser = _repository.Select<User>()
                .FirstOrDefault(u => u.Email == request.Email);

            if (newUser != null)
            {
                throw Errors.User_EmailAlreadyExists;
            }

            newUser = new User
            {
                Email = request.Email,
                Password = _crypto.ComputeHash(request.Password),
                SignupDate = DateTime.UtcNow
            };

            _repository.Insert(newUser);

            var login = CreateLogin(newUser);

            object note = null;
            if (!string.IsNullOrEmpty(request.UnsavedNoteContent))
            {
                var newNote = _repository.SaveNote(request.UnsavedNoteContent, newUser);
                note = newNote.ToItem();
            }

            return Response.Success.WithData(new
            {
                Notes = new[] { note },
                login.Token
            });
        }