Commit 690a4763 authored by Vijay Chaitanya's avatar Vijay Chaitanya Committed by rbonthala-nisum-com

Service side code changes done for MT-78 and MT-82 tasks (#17)

* Service side code changes done for MT-78 and MT-82 tasks

* changes done as per review comments

* code corrected for get domains

* changes done as per new requirement

* as per Review comments corrected the code
parent a6af0bdb
package com.nisum.mytime.controller;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.nisum.mytime.exception.handler.MyTimeException;
import com.nisum.mytime.model.Domains;
import com.nisum.mytime.service.DomainService;
@RestController
public class DomainController {
@Autowired
private DomainService domainService;
@RequestMapping(value = "/domains", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Domains> addDomain(@RequestBody Domains domains)
throws MyTimeException {
domains.setDomainId(domainService.getAllDomains().size()+1+"");
return new ResponseEntity<>(domainService.addDomain(domains), HttpStatus.OK);
}
@RequestMapping(value = "/domains", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<HashMap<Object, Object>>> getAccounts() throws MyTimeException {
return new ResponseEntity<>(domainService.getAllDomains(), HttpStatus.OK);
}
@RequestMapping(value = "/domains", method = RequestMethod.DELETE, produces = MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<String> deleteDomain(@RequestParam String domainId) throws MyTimeException {
domainService.deleteDomain(domainId);
return new ResponseEntity<>("Success", HttpStatus.OK);
}
}
\ No newline at end of file
package com.nisum.mytime.model;
import java.io.Serializable;
import java.util.List;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Document(collection = "Domains")
public class Domains implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private ObjectId id;
private String domainId;
private String domainName;
private String accountName;
private String status;
List<String> employee;
}
package com.nisum.mytime.repository;
import org.springframework.data.mongodb.repository.MongoRepository;
import com.nisum.mytime.model.Domains;
public interface DomainRepo extends MongoRepository<Domains, String> {
}
\ No newline at end of file
/**
*
*/
package com.nisum.mytime.service;
import java.util.HashMap;
import java.util.List;
import com.mongodb.WriteResult;
import com.nisum.mytime.exception.handler.MyTimeException;
import com.nisum.mytime.model.Domains;
/**
* @author Vijay
*
*/
public interface DomainService {
Domains addDomain(Domains d) throws MyTimeException;
List<HashMap<Object,Object>> getAllDomains() throws MyTimeException;
WriteResult deleteDomain(String id) throws MyTimeException;
}
/**
*
*/
package com.nisum.mytime.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import com.mongodb.WriteResult;
import com.nisum.mytime.exception.handler.MyTimeException;
import com.nisum.mytime.model.Domains;
import com.nisum.mytime.model.EmployeeRoles;
import com.nisum.mytime.repository.DomainRepo;
/**
* @author Vijay
*
*/
@Service
public class DomainServiceImpl implements DomainService {
List<Domains> domainList = null;
List<HashMap<Object, Object>> updatedDomainList = null;
List<HashMap<String, String>> updatedEmployeeList = null;
@Autowired
private DomainRepo domainRepo;
@Autowired
private MongoTemplate mongoTemplate;
@Override
public Domains addDomain(Domains d) throws MyTimeException{
return domainRepo.save(d);
}
@Override
public List<HashMap<Object, Object>> getAllDomains() throws MyTimeException {
Query activeDomainListQuery = new Query(Criteria.where("status").is("Y"));
List<Domains> domainList = mongoTemplate.find(activeDomainListQuery, Domains.class);
updatedDomainList = new ArrayList<>();
for (Domains domain : domainList) {
HashMap<Object, Object> domainMap = new HashMap<>();
domainMap.put("id",domain.getId());
domainMap.put("domainName",domain.getDomainName());
domainMap.put("accountName",domain.getAccountName());
domainMap.put("status",domain.getStatus());
updatedEmployeeList = new ArrayList<>();
List<String> employeeIds=domain.getEmployee();
Query query = new Query(Criteria.where("employeeId").in(employeeIds));
List<EmployeeRoles> employeeRoles = mongoTemplate.find(query, EmployeeRoles.class);
for(EmployeeRoles employeesRole : employeeRoles){
HashMap<String, String> employeeMap = new HashMap<>();
employeeMap.put("id",employeesRole.getEmployeeId());
employeeMap.put("name",employeesRole.getEmployeeName());
updatedEmployeeList.add(employeeMap);
}
domainMap.put("deliveryManagers",updatedEmployeeList);
updatedDomainList.add(domainMap);
}
return updatedDomainList;
}
@Override
public WriteResult deleteDomain(String id) throws MyTimeException{
Query query = new Query(Criteria.where("domainId").is(id));
Update update = new Update();
update.set("status","N");
return mongoTemplate.upsert(query, update,Domains.class);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment