Skip to content
Permalink
Browse files

config jwt and complete posts api

  • Loading branch information...
hamed-shirbandi committed Jan 22, 2019
1 parent c09c4a6 commit db3686126db96867419967195ad0046df46a3ccb
@@ -131,7 +131,7 @@ public async Task<PublicJsonResult> CreateAsync(PostInput input)
//بررسی یکتا بودن عنوان مطلب
var existPost = await _postRepository.GetByTitleAsync(input.Title.Trim());
if (existPost != null)
return new PublicJsonResult { result = false, message = Messages.Post_Title_Already_Exist };
return new PublicJsonResult { Result = false, Message = Messages.Post_Title_Already_Exist };

//بررسی نامک -- url friendly
input.UrlTitle = input.UrlTitle.IsNullOrEmptyOrWhiteSpace() ? input.Title.GenerateUrlTitle() : input.UrlTitle.GenerateUrlTitle();
@@ -142,7 +142,7 @@ public async Task<PublicJsonResult> CreateAsync(PostInput input)
var post = _mapperService.BindToDomainModel(input, categoriesId, tagsId);

await _postRepository.CreateAsync(post);
return new PublicJsonResult { result = true, id = post.Id, message = Messages.Post_Create_Success };
return new PublicJsonResult { Result = true, Id = post.Id, Message = Messages.Post_Create_Success };
}


@@ -165,7 +165,7 @@ public async Task<PublicJsonResult> UpdateAsync(PostInput input)
//بررسی یکتا بودن عنوان
var existPost = await _postRepository.GetByTitleAsync(input.Title.Trim());
if (existPost != null && existPost.Id != input.Id)
return new PublicJsonResult { result = false, message = Messages.Post_Title_Already_Exist };
return new PublicJsonResult { Result = false, Message = Messages.Post_Title_Already_Exist };


post.UrlTitle = input.UrlTitle.IsNullOrEmptyOrWhiteSpace() ? input.Title.GenerateUrlTitle() : input.UrlTitle.GenerateUrlTitle();
@@ -183,7 +183,7 @@ public async Task<PublicJsonResult> UpdateAsync(PostInput input)
post.CategoryIds = AddTagsToPost(input.Categories);

await _postRepository.UpdateAsync(post);
return new PublicJsonResult { result = true, message = Messages.Post_Update_Success };
return new PublicJsonResult { Result = true, Message = Messages.Post_Update_Success };


}
@@ -266,7 +266,7 @@ public async Task<PublicJsonResult> DeleteAsync(string id)
}


return new PublicJsonResult { result = true, message = Messages.Post_Delete_Success };
return new PublicJsonResult { Result = true, Message = Messages.Post_Delete_Success };
}


@@ -290,7 +290,7 @@ public async Task<PublicJsonResult> RecycleAsync(string id)
post.IsTrashed = false;
await _postRepository.UpdateAsync(post);

return new PublicJsonResult { result = true, message = Messages.Post_Recycle_Success };
return new PublicJsonResult { Result = true, Message = Messages.Post_Recycle_Success };
}


@@ -70,15 +70,15 @@ public async Task<PublicJsonResult> CreateAsync(TaxonomyInput input)
//بررسی یکتا بودن عنوان
var existTax = await _taxonomyRepository.GetByNameAsync(input.Name.Trim());
if (existTax != null)
return new PublicJsonResult { result = false, message = Messages.Post_Title_Already_Exist };
return new PublicJsonResult { Result = false, Message = Messages.Post_Title_Already_Exist };

//بررسی نامک -- url friendly
input.UrlTitle = input.UrlTitle.IsNullOrEmptyOrWhiteSpace() ? input.Name.GenerateUrlTitle() : input.UrlTitle.GenerateUrlTitle();

var taxonomy = _mapperService.BindToDomainModel(input);

await _taxonomyRepository.CreateAsync(taxonomy);
return new PublicJsonResult { result = true, id = taxonomy.Id, message = Messages.Post_Create_Success };
return new PublicJsonResult { Result = true, Id = taxonomy.Id, Message = Messages.Post_Create_Success };
}


@@ -100,7 +100,7 @@ public async Task<PublicJsonResult> UpdateAsync(TaxonomyInput input)
//بررسی یکتا بودن عنوان
var existTax = await _taxonomyRepository.GetByNameAsync(input.Name.Trim());
if (existTax != null && existTax.Id != input.Id)
return new PublicJsonResult { result = false, message = Messages.Post_Title_Already_Exist };
return new PublicJsonResult { Result = false, Message = Messages.Post_Title_Already_Exist };


//بررسی نامک -- url friendly
@@ -110,7 +110,7 @@ public async Task<PublicJsonResult> UpdateAsync(TaxonomyInput input)


await _taxonomyRepository.UpdateAsync(tax);
return new PublicJsonResult { result = true, message = Messages.Post_Update_Success };
return new PublicJsonResult { Result = true, Message = Messages.Post_Update_Success };


}
@@ -1,16 +1,14 @@
using System;
using CorMon.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Text;

namespace CorMon.Application.Users.Dto
{
public class UserOutput
public class UserOutput: JwtBaseModel
{
public string Id { get; set; }
public string DisplayName { get; set; }
public string About { get; set; }

public string UserName { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string AvatarUrl { get; set; }
@@ -12,5 +12,6 @@ public interface IUserService
Task<UserOutput> GetAsync(string id);
Task<PublicJsonResult> UpdateAsync(UserInput input);
Task<UserOutput> GetSampleUserAsync();
Task<UserOutput> GetByEmailAsync(string email);
}
}
@@ -53,7 +53,24 @@ public async Task<UserOutput> GetAsync(string id)
}





/// <summary>
///
/// </summary>
public async Task<UserOutput> GetByEmailAsync(string email)
{
var user = await _userRepository.GetByEmailAsync(email);
if (user == null)
{
throw new Exception("User not found");
}

return _mapperService.BindToOutputModel(user);
}




/// <summary>
///
@@ -90,7 +107,7 @@ public async Task<PublicJsonResult> UpdateAsync(UserInput input)
user.UserName = input.UserName;
await _userRepository.UpdateAsync(user);

return new PublicJsonResult { result=true,message=Messages.User_Update_Success};
return new PublicJsonResult { Result=true,Message=Messages.User_Update_Success};
}


@@ -13,5 +13,6 @@ public interface IUserRepository
User Get(string id);
Task UpdateAsync(User user);
Task<User> GetFirstUserAsync();
Task<User> GetByEmailAsync(string email);
}
}
@@ -18,7 +18,7 @@ public static bool Has<T>(this IList<string> collections, string name = "")
var collection = name;
if (string.IsNullOrEmpty(collection))
{
collection = typeof(T).Name.ToLower();
collection = typeof(T).Name;

if (!collection.EndsWith("s"))
collection = collection + "s";
@@ -3,6 +3,6 @@
public class JwtBaseModel
{
public string UserName { get; set; }
public string UserId { get; set; }
public string Id { get; set; }
}
}
@@ -6,8 +6,9 @@ namespace CorMon.Core.JsonModels
{
public class PublicJsonResult
{
public string message { get; set; }
public bool result { get; set; }
public string id { get; set; }
public string Message { get; set; }
public bool Result { get; set; }
public string Id { get; set; }
public string Token { get; set; }
}
}
@@ -44,6 +44,18 @@ public async Task<User> GetAsync(string id)




/// <summary>
///
/// </summary>
public async Task<User> GetByEmailAsync(string email)
{
return await _users.Find(e => e.Email == email).FirstOrDefaultAsync();
}




/// <summary>
///
/// </summary>

Some generated files are not rendered by default. Learn more.

@@ -132,6 +132,9 @@
<data name="Post_Update_Success" xml:space="preserve">
<value>عملیات ویرایش پست با موفقیت انجام شد</value>
</data>
<data name="User_Login_Failed" xml:space="preserve">
<value>نام کاربری یا رمز عبور اشتباه است</value>
</data>
<data name="User_Update_Success" xml:space="preserve">
<value>عملیات ویرایش کاربر با موفقیت انجام شد</value>
</data>
@@ -0,0 +1,80 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using CorMon.Core.Domain;
using CorMon.Application.Users;
using CorMon.Web.Api.Services.Jwt;
using CorMon.Core.JsonModels;
using CorMon.Resource;
using CorMon.Web.Api.Models;

namespace CorMon.Web.Api.Controllers
{
public class AccountController : BaseController
{
#region Fields

private readonly SignInManager<User> _signInManager;
private readonly UserManager<User> _userManager;
private readonly IUserService _userService;
private readonly IJwtService _jwtService;



#endregion

#region Ctor

public AccountController(IUserService userService, IJwtService jwtService, UserManager<User> userManager, SignInManager<User> signInManager)
{
_jwtService = jwtService;
_userService = userService;
_userManager = userManager;
_signInManager = signInManager;
}


#endregion

#region Public Methods



/// <summary>
///
/// </summary>
[HttpPost]
[Route("account/login")]
public async Task<PublicJsonResult> Login([FromBody] LoginViewModel model)
{

var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, false, false);

if (!result.Succeeded)
return new PublicJsonResult { Result = false, Message = Messages.User_Login_Failed };

var user = await _userService.GetByEmailAsync(model.Email);


return new PublicJsonResult { Result = true, Token = _jwtService.GenerateJwtToken(user) };
}


#endregion


#region Private Methods






#endregion

}


}
@@ -1,4 +1,8 @@
using Microsoft.AspNetCore.Mvc;
using CorMon.Core.Extensions;
using CorMon.Core.Helpers;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Security.Claims;

namespace CorMon.Web.Api.Controllers
{
@@ -29,14 +33,49 @@ public BaseController()





#endregion

#region protected Methods



/// <summary>
///
/// </summary>
protected string GetCurrentUserName()
{
if (this.User == null)
return "";
ClaimsIdentity claimsIdentity = this.User.Identity as ClaimsIdentity;
if (claimsIdentity.Claims.Count() == 0)
return "";
var userName = claimsIdentity.Claims.FirstOrDefault(c => c.Type == nameof(JwtBaseModel.UserName).ToLowerFirst()).Value;
return userName;

}



/// <summary>
///
/// </summary>
protected long GetCurrentUserId()
{
if (this.User == null)
return 0;

ClaimsIdentity claimsIdentity = this.User.Identity as ClaimsIdentity;
if (claimsIdentity.Claims.Count() == 0)
return 0;
var userId = claimsIdentity.Claims.FirstOrDefault(c => c.Type == nameof(JwtBaseModel.Id).ToLowerFirst()).Value;
return long.Parse(userId);

}





#endregion

0 comments on commit db36861

Please sign in to comment.
You can’t perform that action at this time.