Exemple #1
0
        public IActionResult IncrementViews(int id)
        {
            try
            {
                BlogPost blog = _context.Blogs.FirstOrDefault(b => b.Id == id);

                if (blog == null)
                {
                    throw new Exception($"No blog found! ID: {id}");
                }

                else
                {
                    blog.Views += 1;
                    _context.Entry(blog).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                    _context.SaveChanges();

                    return(Ok(new BasicHttpResponse <object> {
                        Ok = true
                    }));
                }
            }
            catch (Exception e)
            {
                return(Ok(new BasicHttpResponse <object> {
                    Ok = false, Message = e.Message
                }));
            }
        }
Exemple #2
0
        public async Task <IActionResult> UpdateUser([FromBody] UserAccount user)
        {
            try
            {
                var u = _context.Users.FirstOrDefault(x => x.Email == user.Email);

                DateTime now = DateTime.Now;

                if (u == null)
                {
                    // New user. Assign user name and save.
                    int idNum = _context.Users.Any()
                        ? _context.Users.Max(x => x.Id) + 1000
                        : 1000;

                    u = new UserAccount
                    {
                        Username     = $"FivePeaker_{idNum}",
                        Email        = user.Email,
                        CreationDate = now,
                        LastLogin    = now
                    };

                    await _context.Users.AddAsync(u);
                }
                else
                {
                    u.LastLogin = now;

                    if (!string.IsNullOrEmpty(user.Username) && user.Username != u.Username)
                    {
                        //User is setting custom username
                        if (_context.Users.Any(x => x.Username == user.Username))
                        {
                            throw new Exception("Username is taken!");
                        }

                        if (user.Username.Length < 3)
                        {
                            throw new Exception("Username too short!");
                        }

                        if (user.Username.Length >= 30)
                        {
                            throw new Exception("Username too long!");
                        }

                        u.Username = user.Username;
                    }

                    _context.Entry(u).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                }

                await _context.SaveChangesAsync();

                return(Ok(new BasicHttpResponse <UserAccount> {
                    Ok = true, Data = u
                }));
            }
            catch (Exception e)
            {
                return(Ok(new BasicHttpResponse <UserAccount> {
                    Ok = false, Message = e.Message
                }));
            }
        }